foreach
According to the official documents, foreach syntax (sugar) appears to replace the iterator iteration of a collection. The purpose is to make the code beautiful and reduce errors. Started with java 1.5.
Official website link: https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
Note: the Collection Interface (that is, the Collection we often call) includes not only Java util. The Collection also includes Java util. Map. Although map is not a real Collection, the map interface includes a series of Collection view operations, which enable them to be regulated as collections
In a word, Collection (lowercase) includes the implementation classes under the Collection interface and Map interface, as shown in blue in the figure below.
Iterator
The iterator interface appears to traverse a collection and replace the Enumeration interface. Its main advantage is that it can delete elements while traversing. Started with java 1.2.
* An iterator over a collection. {@code Iterator} takes the place of * {@link Enumeration} in the Java Collections Framework. Iterators * differ from enumerations in two ways: * * <ul> * <li> Iterators allow the caller to remove elements from the * underlying collection during the iteration with well-defined * semantics. * <li> Method names have been improved. * </ul>
Iterable
The Iterable interface appears to allow object s other than arrays and collections to perform foreach loop operations. Started with java 1.5
* Implementing this interface allows an object to be the target of * the "for-each loop" statement. See * <strong> * <a href="{@docRoot}/../technotes/guides/language/foreach.html">For-each Loop</a> * </strong>
Sort it out
The Iterator interface is in Java 1 2, and foreach syntax and iteratable interface java1 5 at the same time. Development sequence:
- In order to delete the Iterator when traversing the interface;
- In order to simplify the Iterator code, foreach syntax appears;
- In order to make foreach traverse object s other than arrays and collections, iteratable appears.
ending
There are two types of objects that use foreach loops:
- array
- Implementation class of Iterable interface
For arrays, foreach takes a for loop
For the implementation class of Iterable interface, foreach adopts iterator method
There is an iterator() method in the iteratable interface, which returns an Iterator object. After java 1.5, the Iterator interface not only serves collection, but any implementation class of the iteratable interface should implement the iterative operation in the Iterator interface.
Take the source code of a collection as an example:
// Implementation of iterator method public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { ... // The iterator() method in the Iterable interface must be overridden @override public Iterator<E> iterator() { return new Itr(); } ... } // Specific implementation of iterative operation private class Itr implements Iterator<E> { // You must override the methods of the three iterative operations in the Iterator interface @override boolean hasNext(){...}; @override E next(){...}; @override void remove(){...}; }
above.
Iteratable represents a flag that indicates that the class can be iterated. It provides an iterator. If the user needs to iterate, he will call his iterator() method
Iterator defines the concrete implementation of iterative operations.
These two interfaces separate the use and implementation of iterators to achieve decoupling.
Back to foreach, the emergence of foreach is to simplify the iterator, so all that can use the iterator can be foreach. Further, all that implement the Iterable interface can be foreach, and the underlying layer is the iterator of the iterator method defined in the Iterable interface. What about the unfulfilled foreach? For example, arrays are not implemented. Array foreach, which goes back to the combing part. Its foreach bottom layer uses a general for loop.
Therefore, foreach is like a syntax sugar, and the underlying implementation is still a for loop and iterator.