Analysis of Java collection collector iterator method

Time:2020-7-31

This article mainly introduces the Java collection collector iterator method analysis, the article through the example code introduction is very detailed, to everybody’s study or the work has certain reference study value, needs the friend may refer to

Overview of iterator interface

/**
 * java.util.Iterator Interface: selector (traversing Collection)
 *There are two common methods
 *          boolean hasNext()
 *Returns true if there are still elements that can be iterated.
 *That is to judge whether there are any next elements in the set. If there are any, return true and if not, return false
 *          E next()
 *Returns the next element of the delegate.
 *The next element in the collection is taken out
 *Iterator iterator iterator is an interface that we cannot use directly. We need to use the implementation class object of iterator interface.
 *The way to get the implementation class is special. There is a method in the collection interface called iterator (), which returns the implementation class object of the sender
 *Iterator < E > iterator() returns the generator that iterates over the elements of the collection collection.
 *
 *How to use iterators (key points)
 *1. Use the method iterator() in the collection to get the implementation class object of the sender, and use the iterator interface to receive (polymorphic)
 *2. Use the method hasnext in iterator interface to determine whether there is a next element
 *3. Use the method next in iterator interface to get the next element in the collection
 */

Implementation of iterator code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;

public class Demo01Iterator {
  public static void main(String[] args) {
    Collection <String> collection = new ArrayList<>();

    collection.add (No. 1); and;
    collection.add (No. 2); and;
    collection.add (No. 3); and;
    collection.add ("No.4");
    collection.add (No. 5); and;

    //Step 1: use the method iterator () in the collection to get the implementation class object of the sender
    //Step 2: use iterator interface to receive (polymorphic)
    Iterator<String> stringIterator = collection.iterator();

    //Step 3: use the method hasnext in the iterator interface to determine whether there is a next element
    while (stringIterator.hasNext()) {
      //Step 4: use the method next in the iterator interface to extract the next element in the collection
      System.out.println(
          stringIterator.next()
      );
    }
    
  }
}
Output results:
No. 1
No. 2
No. 3
No. 4
No. 5

Realization principle of iterator

New a collection
collection = new ArrayList<>()


Suppose you add elements to the collection
Collection = [element 1, element 2, element 3, element 4, element 5]


The index of the element in the collection
Index = [0, 1, 2, 3, 4]


Call iterator method
collection.iterator()
Function: get the implementation class object well of iterator and point the pointer (index) to the - 1 prime index of the collection


Code Description:
while (stringIterator.hasNext()) {
  stringIterator.next()
}
Through the hasnext () method of iterator interface, we can determine whether the collection has the next element
If there is a next element, the next () method of the iterator interface is called to extract the next element, and the pointer is moved back one bit.


In this way, from the next index in the - 1 position of the index, we start to get the elements until we get them completely.

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.