Iterator and Iterable in Java are key interfaces in the Java collection framework, which provide convenience for traversing collections. In this article, PHP editor Xinyi will deeply analyze the working principles of Iterator and Iterable in Java to help readers better understand the traversal mechanism of Java collections.
hasNext()
: Check if there are more elements in the collection. next()
: Returns the next element in the collection. Iterator interface also defines some optional methods, such as the remove()
method, which is used to remove the current element from the collection.
You can use the following steps to traverse a collection using the Iterator interface:
hasNext()
method to check if there are more elements in the collection. next()
method to get the next element. The following is an example of using the Iterator interface to traverse an ArrayList:
import java.util.ArrayList; import java.util.Iterator; public class IteratorExample { public static void main(String[] args) { // 创建一个 ArrayList ArrayList<String> names = new ArrayList<>(); // 向 ArrayList 中添加一些元素 names.add("John"); names.add("Mary"); names.add("Bob"); // 获取 ArrayList 的 Iterator 对象 Iterator<String> iterator = names.iterator(); // 使用 Iterator 对象遍历 ArrayList while (iterator.hasNext()) { String name = iterator.next(); System.out.println(name); } } }
The Iterable interface is a high-level interface in the Java collection framework for traversing collections. It defines an iterator()
method that returns an Iterator object.
You can use the following steps to traverse a collection using the Iterable interface:
iterator()
method to obtain an Iterator object. The following is an example of using the Iterable interface to traverse an ArrayList:
import java.util.ArrayList; import java.util.Iterable; public class IterableExample { public static void main(String[] args) { // 创建一个 ArrayList ArrayList<String> names = new ArrayList<>(); // 向 ArrayList 中添加一些元素 names.add("John"); names.add("Mary"); names.add("Bob"); // 获取 ArrayList 的 Iterable 对象 Iterable<String> iterable = names; // 使用 Iterable 对象获取一个 Iterator 对象 Iterator<String> iterator = iterable.iterator(); // 使用 Iterator 对象遍历 ArrayList while (iterator.hasNext()) { String name = iterator.next(); System.out.println(name); } } }
Iterator and Iterable interfaces are both interfaces for traversing collections, but there are some differences between them.
hasNext()
and next()
. iterator()
. Generally speaking, if you need to directly operate the elements in the collection, you can use the Iterator interface. If you only need to iterate over the elements in a collection, you can use the Iterable interface.
For example, if you need to remove elements from a collection, you can use the remove()
method of the Iterator interface. If you only need to traverse the elements in the collection, you can use the iterator()
method of the Iterable interface to obtain an Iterator object, and then use the Iterator object to traverse the collection.
The above is the detailed content of Java Iterator and Iterable: An in-depth analysis of Java collection traversal mechanism. For more information, please follow other related articles on the PHP Chinese website!