Iterator interface
The iterator interface is also a member of the Java Collection framework, but it is not the same as the collection of the collection series, the map series: The collection Series collection, the Map series collection is primarily used to load other objects, The iterator is used primarily to traverse the elements in the collection collection, and the iterator object is also known as an iterator.
The iterator interface hides the underlying details of the various collection implementation classes, providing the application with a unified programming interface that iterates through the collection collection elements. The following three methods are defined in the iterator interface:
Boolean Hasnext (); If the collection element being iterated has not been traversed, return trueobject next (); Returns the next element in the collection void Remove (); Delete the previous next method in the collection returns the element
The following program demonstrates traversing a collection element through the iterator interface.
ImportJava.util.*; Public classiteratortest{ Public Static voidMain (string[] args) {//Create a collectionCollection books =NewHashSet (); Books.add ("Lightweight Java EE Enterprise Application Combat"); Books.add ("Crazy Java Handout"); Books.add ("Crazy Android Handout"); //gets the iterator that corresponds to the books collectionIterator it =Books.iterator (); while(It.hasnext ()) {//the data type returned by the It.next () method is of type object,//forced type conversion requiredString book =(String) it.next (); SYSTEM.OUT.PRINTLN (book); if(Book.equals ("Crazy Java Handout"))) { //removes the element returned from the previous next method from the collectionIt.remove (); } //Assigning a value to a book variable does not alter the collection element itselfBook = "Test string";//①} System.out.println (books); }}
Show Results:
Lightweight Java EE Enterprise app crazy Android handout crazy Java handout [lightweight Java EE Enterprise Application combat, Crazy Android Handout]
Java Foundation--java Collection (ii)