First, In
troduction to Iterator:
1, java.util.Iterator is an interface, it only provides an iterative basic rule, providing a way to access each element in a container object without exposing the internal details / underlying structure of the object . In the JDK he is defined like this: an iterator that iterates over the collection. As you can see from the definition, the Iterator mode is also called the Cursor mode. It can abstract the access logic from different types of collection classes, thus avoiding 2, exposing the internal structure of the collection to the client.
2. Iterations In fact, we can simply understand traversal, which is a standard access method for traversing all objects in various types of containers. It is a typical design pattern. Obviously, access to a container object necessarily involves a traversal algorithm. You can plug the traversal method into the container object one by one; or don't provide any traversal algorithm at all, let the person using the container implement it yourself. Both of these situations seem to solve the problem.
package java.util;
public interface Iterator<E>
{
boolean hasNext();
E next();
void remove();
}
Second, Iterator usage
The Iterator function in Java is relatively simple and can only be traversed one way forward:
(1) The container calls iterator() to require the collection class container to return an Iterator object. Note: Many collection classes implement the java.lang.Iterable interface so that the container object can call the iterator() method.
(2) hasNext() Checks if there are any more elements in the container.
(3) next() gets the next element in the container. On the first call, it returns the first element of the container.
(4) remove() removes the newly returned element of the iterator.
List<String> strList = new ArrayList<>();
//1、using for-each loop
for(String str : strList)
System.out.println(str);
//2、using iterator
Iterator<String> it = strList.iterator();
while(it.hasNext())
{
String str = it.next();
System.out.println(str);
}
Iterator is the simplest implementation of Java iterators. The ListIterator designed for List has more functions. It can traverse the List from two directions, and can also insert and delete elements from the List.
Third, Fail-Fast (fast failure) mechanism
What we know is that ArrayList is thread-insecure. If there are other threads that modify the List during the iterator process, it will throw a ConcurrentModificationException. This is the Fail-Fast mechanism, in order to prevent unsafe operations of iterations under multithreading.
So what is the meaning of a quick failure?
After the ArrayList class creates an iterator, unless the list structure is modified by the iterator itself remove or add, the list is modified in any other way in other threads, and the iterator will immediately throw an exception and fail quickly.
Fourth, Iterator advanced
1. What is the difference between enumerating the Enumeration and Iterator interfaces?
Enumeration is twice as fast as iteration, using very little memory. Enumerations are suitable for basic needs. But the Iterator is more secure because it always rejects other threads to modify the object it is iterating over the collection.
2. What is the difference between Iterator and listIterator?
We can use the iterator Iterator to iterate over the Set and List collections, while the ListIterator can only use List.
The iterator traversal has only the forward direction, and the ListIterator can be used to traverse in both directions.
ListIterator inherits the Iterator interface and is equipped with additional features such as adding elements, replacing an element, and getting the index position of the previous and next elements.
3. What is the difference between fail-fast and fail-safe?
The Iterator's fail-fast property works in conjunction with the current collection, so it is not affected by any changes in the collection. All collection classes in the Java.util package are designed to fail-fast, while the collection classes in java.util.concurrent are fail-safe. The Fail-fast iterator throws a ConcurrentModificationException when it detects that the structure of the collection being traversed is changed, and the fail-safe iterator never throws a ConcurrentModificationException.
4. What is the advantage of Iterator?
- An iterator can provide a uniform iteration.
- Iterators can also provide a variety of different iterations when transparent to the client.
- Iterators provide a fast failure mechanism to prevent unsafe operations of iterations under multithreading.
Five, foreach loop
1. Introduction: Java 5.0 introduces a new and more concise foreach syntax for array and container traversal, also known as enhanced for loop, which provides great convenience for developers.
It can traverse arrays or containers without using subscripts, and foreach will automatically generate each element. The basic syntax is as follows:
For(type element:obj)
{
Statement block containing elemengt //loop body
}
Type: element type elemengt : element variable obj: traverse object, can be an array or a collection container
List<String> strList = new ArrayList<>();
strList.add("111");
strList.add("222");
strList.add("333");
for(String str : strList)
System.out.println(str);
2, the scope of application: For any container that implements the Iterable interface can use a foreach loop. There can be two types after the colon of the foreach syntax: one is an array, and the other is a class that implements the Iterable interface.
3, the shortcomings of the For-Each loop: lost the index information.
When traversing a collection or array, if you need to access the subscript of a collection or array, it is best to use the old way to implement looping or traversing instead of using the enhanced for loop because it loses the subscript information.