Understanding Java Iterator

后端 未结 3 1717
我寻月下人不归
我寻月下人不归 2021-01-20 18:50

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 st

相关标签:
3条回答
  • 2021-01-20 19:22

    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");
          }
    }
    
    0 讨论(0)
  • 2021-01-20 19:30

    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.

    0 讨论(0)
  • 2021-01-20 19:31
    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
    
    0 讨论(0)
提交回复
热议问题