3

If I run the following code, it will print out 3 times duplicate, but when I remove the if statement inside the while loop (just to see how many times it will iterate) it starts an infinite loop.

How does actually this hasNext() method working? I thought that will iterate only 5 times as I have 5 items in the list.

    public class ExerciseOne {
    public static void main(String []args){
        String []colors = {"MAGENTA","RED","WHITE","BLUE","CYAN"};
        List<String> list = new ArrayList<String>();
        for(String color : colors)
            list.add(color);
        String[] removeColors = {"RED","WHITE","BLUE"};
        List<String> removeList = new ArrayList<String>();
        for(String color : removeColors)
            removeList.add(color);

        removeColors(list,removeList);
        System.out.printf("%n%nArrayList after calling removeColors:%n");
        for(String color : list)
        {
            System.out.printf("%s ",color);
        }
    }

    private static void removeColors(Collection<String> collection1, Collection<String> collection2)
    {
        Iterator<String> iterator = collection1.iterator();

            while(iterator.hasNext()){
                if(collection2.contains(iterator.next()))
                    System.out.println("duplicate");
            }
    }

}
6
  • 5
    Did you read the documentation? Commented Jan 13, 2016 at 19:57
  • 1
    hasNext does not move iterator cursor, iterator.next() moves it, so when you've removed condition you actually removed cursor change too Commented Jan 13, 2016 at 20:02
  • Yes of course, "Returns true if the iteration has more elements", but I cant understand when It reaches the end of the list why it continues the loop, I will try to find some other tutorial or documentation because that one from oracles' is not that clear for me Commented Jan 13, 2016 at 20:05
  • check answer of @leo its correct Commented Jan 13, 2016 at 20:06
  • 3
    @imoteb, it doesn't reach the end of the list. If you remove the if condition you never move the iterator forward at all, it's just stuck at the start of the list forever. Commented Jan 13, 2016 at 20:06

3 Answers 3

10

It is pretty simple, actually

while(iterator.hasNext()){
    if(collection2.contains(iterator.next()))
       System.out.println("duplicate");
}

Imagine that the iterator is a pointer to an element of your list.

When you call next(), you're moving this pointer one step ahead.

If you don't move the pointer, hasNext() will always be true because you're still in the beginning of the list.

So you have to call the iterator's next() until there isn't any remaining element in the list.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you , now I got it
@leo you say it is LIKE a pointer ("imagine"). But, is it a pointer?
iterator object is not a pointer to an element of the list. If it was the case, there is no need for iterator's datatype to be consistent with that of the collection (eg list).
1

If you remove the if statement, then it will go for an infinite loop since your iterator.next() is in the if condition. Actually iterator.next() is the api that moves the pointer, not the hasNext(). hasNext() just checks if there is any element in the collection. Since removal of the if statement is also removing the hasNext api, the pointer to the collection is not moving and hasNext is always returning true.

If you take out the iterator.next() from the if condition and move it above the if condition, then the loop will iterate for 5 times even after you remove the if statement.

Iterator<String> iterator = collection1.iterator();
while(iterator.hasNext()){
      String currentColor = iterator.next();
      if(collection2.contains(currentColor)){
         System.out.println("duplicate");
      }
}

2 Comments

Thanks Erick for pointing that. That was my first answer ever in SO. I edited my answer based on your comment, although it has been explained properly in the other answer.
Thanks a lot @Erick.
1
The question why Iterator is important/introduced is simple:
consider following example:
List<String> list = new ArrayList<String>();
list.add("Anurag");
list.add("Soni");
list.add("MMM");
list.add("GKP");

for(string s : list){
  if(s.equals(" Anurag")
  s.remove();
  System.out.println(s);
}

This will throw an exception-`Concurrent Modification exception` as you are trying to alter the structure of the data structure List before the iteration is completed.

so you may use Iterator for the same purpose .
Iterator iterator = List.iterator();

while(iterator.hasNext()){
String current = iterator.next();
if(current=="Anurag"){
  iterator.remove();
}else{
System.out.println(current);
}

}
OUTPUT:  Soni
         MMM
         GKP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.