Iterator interface:
So the container class that implements the collection interface has a iterator method to return an object that implements the iterator interface.
The iterator object is called an iterator to facilitate the traversal of elements within a container.
The iterator interface defines the following methods:
Boolean hasnext (); To determine if there are elements on the right side of the cursor
Object Next (); Returns the element to the right of the cursor and moves the cursor to the next position
void Remove (); Deletes the element to the left of the cursor, which can only be executed once after the next action is executed
Practice:
Import java.util.*;
public class testiterator{
public static void Main (string[] args) {
Collection C = new HashSet ();
C.add ("Hello");
C.add ("World");
C.add ("!");
Iterator i = C.iterator ();
while (I.hasnext ()) {
string n = (string) i.next ();
System.out.println (n);
}
}
}
Device: an instance of a series of classes provided by the Java API that is used to store objects in a program. The container class provided by the JDK is in the Java.util package. The collection interface defines methods for accessing a set of objects, and its Sub-interface set and list define the storage mode respectively.
The data objects in the set are not ordered and cannot be duplicated.
The data objects in the list are in order and can be duplicated.
The map interface defines a method for storing "key (value) mapping pairs"
Collection The method defined in the interface:
int size (); How many elements
Boolean IsEmpty ();//IS NULL
void Clear ();
Boolean contains (object element);//Whether or not to contain an object
Boolean Add (object element);//Add Element
Boolean remove (object element);//delete
Iterator iterator ();
Boolean Containsall (collection C);//is not containing the elements in another collection
Boolean AddAll (collection C);//Add All
Boolean RemoveAll (collection C);//Delete all
Boolean Retainall (collection C);//Find Intersection
Object[] ToArray ();//Convert to object class array
The collection is loaded with objects and cannot be the underlying data type. Because the underlying data type is stored on the stack, it can be retracted at any time.
The Remove () method calls the Equals () method, so the class that you implement overrides the Equals method and the Hashcode () method.