An iterator inside the Iterator:java that is used primarily to take the value inside the collection container.
Iterator Common methods:
1. Iterator (): Requires the container to return a iterator;
2. Hasnext (): Checks whether there are any elements that are not traversed in the collection;
3. Next (): Gets the next element in the collection;
4. Remove (): Removes the newly returned element of the iterator;
Iterator using:
set<string> iteset = new hashset<string> (); New HashSet Gather
Iteset.add ("first"); Add elements
Iteset.add ("second");
Iteset.add ("third");
Iterator<string> iter = Iteset.iterator (); Get the Iterator
while (Iter.hasnext ()) {//Iterator have next element
String str = (string) iter.next (); Get the next element in Iterator
System.out.println (str);
}
Note: The collection list and set inherit from the interface collection. There are two elements to get a collection in a list: 1. Iterator iterator traversal; 2. Get (int index); There is only one way to get the elements in the set collection: iterator iterator traversal, there is no method such as get (int index) in the set class.
Iterator (iterator)