Summary
Iterators provide the ability to traverse a collection without understanding its internal implementation.Container interiors can be isolated and decoupled from traversal operations.
Implementing a simple set using an iterator
Master an iterator by customizing a simple set and traversing it using an iterator.
Collection Description
A simple set with the following rules
- 1. Only three strings can be stored
- 2. If the fourth data is inserted, the first position will be overwritten.
Implement Interface Description
Iterable interface description
- If you want to traverse a collection with foreach, you must implement the interface.(Reasons explained later)
- iterator(): is an interface that must be implemented and returns an iterator.
Iterator
- An iterator that traverses a known set.
- hasNext(): Must be implemented, returning a boolean to indicate whether there is a next value
- next(): must be implemented, returning the next element from the collection.
code implementation
Custom Collection
/** * Custom Collection * Function: Only three elements can be inserted, and inserting the fourth element replaces the first element in the collection * To implement the Iterable interface, you must implement the iterator method and return an iterator **/ public class MyCollection implements Iterable<String> { String value1; String value2; String value3; int currentValueIndex = 1; public String get(int index) { switch (index) { case 1: return value1; case 2: return value2; case 3: return value3; default: return null; } } /** * Add an element, currentValueIndex is the current element index, indicating the location of the next insert * Maintains a collection that can only insert three String s * @param value * @return */ public boolean add(String value) { switch (currentValueIndex) { case 1: value1 = value; currentValueIndex++; break; case 2: value2 = value; currentValueIndex++; break; case 3: value3 = value; currentValueIndex-=2; break; default: break; } return true; } /** * Return to our own defined set iterator * @return */ @Override public Iterator<String> iterator() { return new MyCollectionIterator(this); } }
Custom Iterator
An iterator has been customized to traverse the custom collection above.
/** * Implement an iterator for a collection I implement * Methods that must be implemented * hasNext Is there the next element * next Remove the next element */ public class MyCollectionIterator implements Iterator<String> { int index = 1; int maxIndex = 3; MyCollection myCollection; public MyCollectionIterator(MyCollection myCollection) { this.myCollection = myCollection; } /** * If the current pointer already points to 3, there is no next pointer, and false is returned * Otherwise there will be another * @return */ @Override public boolean hasNext() { if (index > maxIndex) { return false; } return true; } /** * Remove the next element and move the pointer one step forward * @return */ @Override public String next() { String result = myCollection.get(index); index ++; return result; } }
test method
public class Test { public static void main(String[] args) { MyCollection collection = new MyCollection(); collection.add("test1"); collection.add("test2"); collection.add("test3"); /** * Loop 1, get MyCollection's iterator, and iterate through our custom collection with while, next, hasNext */ Iterator<String> iterator = collection.iterator(); while (iterator.hasNext()) { String str = iterator.next(); System.out.println(str); } /** * Loop 2, because our collection implements the Iterable interface, we can use a forEach loop * But foreach loop compiles the same code as loop 1 */ for (String s : collection) { System.out.println(s); } } }
Test Code Decompilation Results
Here is the decompiled code for the above test code. As you can see, loop 2 (foreach) above has been compiled into the same structure as loop 1.Here you can see that foreach loops actually use the iterator() method to get iterators to complete the traversal.Therefore, if you want to use foreach loops, you must implement the Iterable interface.
public class Test { public Test() { } public static void main(String[] args) { MyCollection collection = new MyCollection(); collection.add("test1"); collection.add("test2"); collection.add("test3"); Iterator iterator = collection.iterator(); while(iterator.hasNext()) { String str = (String)iterator.next(); System.out.println(str); } Iterator var5 = collection.iterator(); while(var5.hasNext()) { String s = (String)var5.next(); System.out.println(s); } } }