Iterator interface
1. Iterator interface
Iterable
Built-in method iterator() returns a newly created Iterator.
like:
public interface Iterable { Iterator Iterator(); } Iterator has two methods: hasNext() and next() to implement. public interface Iterator { boolean hasNext(); Item next(); void remove(); //optional implementation}
2. Implement import
import java.util.Iterator;
Generic classes, implement Iterable interface implements Iterable<Item>
Implements Iterable's iterator() method, returning any defined iterator type.
Define iterator type implements Iterator< Item >
Implement hasNext(), next(), and remove()
3. Example:
public class Stack<Item> implements Iterable<Item> { public Iterator<Item> iterator() { return new ListIterator<Item>(first); } private class ListIterator<Item> implements Iterator<Item> { private Node<Item> current; public ListIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; } }} 4. Calling methods
foreach iteration
for (String s: stack) System.out.println(s);
If it is a basic type such as int/double, please use the previous conversion relationship
5. Looping traversal
Iterator i = stack.iterator(); while (i.hasNext()) { String s = i.next(); }Enumeration interface
Enumeration is an interface class in java.util. In Enumeration, it encapsulates methods about enumerating data sets. It is similar to Iterator. It is used to traverse elements in the collection. However, enumeration Enumeration only provides the function of traversing elements of the Vector and Hashtable type collections. This type of collection object obtains an Enumeration object by calling the elements() method and then calls the following methods to traverse elements in the collection.
hasMoreElements(): determines whether there is still data in the Enumeration object
nextElement(): Get the next data in the Enumeration object
Examples are as follows:
Enumeration req = request.getParameterNames(); while (req.hasMoreElements()) { Object obj = (Object) req.nextElement(); if (obj.toString().trim().equals("LastPage")) { System.out.println("LastPage /n"); } else if (obj.toString().trim().equals("NextPage")) { System.out.println("NextPage"); } } The difference between Iterator and Enumeration
In Java collections, we usually traverse the collection through "Iterator" or "Enumeration class". Today, let’s learn about the differences between them together.
Let’s first look at the source codes of Enumeration.java and Iterator.java, and then talk about their differences.
Enumeration is an interface, and its source code is as follows:
package java.util;public interface Enumeration<E> { boolean hasMoreElements(); E nextElement();} Iterator is also an interface, and its source code is as follows:
package java.util;public interface Iterator<E> { boolean hasNext(); E next(); void remove();} After reading the code, let’s talk about the difference between them.
(01) Different function interfaces
Enumeration has only 2 function interfaces. Through Enumeration, we can only read the data of the collection, but cannot modify the data.
Iterator has only 3 function interfaces. In addition to reading the data of the collection, iterator can also delete the data.
(02) Iterator supports the fail-fast mechanism, but Enumeration does not.
Enumeration is an interface added by JDK 1.0. The functions used include Vector, Hashtable and other classes, which are all added in JDK 1.0. The purpose of Enumeration is to provide them with a traversal interface. Enumeration itself does not support synchronization, but when Vector and Hashtable implement Enumeration, synchronization is added.
Iterator is an interface added only in JDK 1.2. It also provides traversal interfaces for collections such as HashMap and ArrayList. Iterator supports the fail-fast mechanism: when multiple threads operate on the content of the same collection, a fail-fast event may be generated.