Home  >  Article  >  Java  >  Java Iterator and Iterable from beginner to proficient

Java Iterator and Iterable from beginner to proficient

WBOY
WBOYforward
2024-02-20 08:27:34674browse

Java Iterator 和 Iterable 的从入门到精通

Iterator and Iterable play an important role in Java CollectionsFramework. Iterator provides a standard way to traverse the elements of a collection, while Iterable provides a mechanism for creating Iterators. This article will introduce the usage of Iterator and Iterable in detail from Getting Started to Mastery, helping readers master the usage skills of these two interfaces, so as to better operate Java collections.

Iterator and Iterable Overview

Iterator

The Iterator interface defines an iterator that allows programmers to access elements in a collection in order. Iterator provides some basic methods, including hasNext(), next(), and remove(), for checking whether the next element exists, getting the next element, and removing the current element from the collection.

public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}

Iterable

The Iterable interface defines an iterable object that can generate an Iterator instance to traverse its elements. Iterable provides a basic method, iterable(), which returns an Iterator instance.

public interface Iterable<E> {
Iterator<E> iterator();
}

Usage of Iterator and Iterable

Use Iterator

To use Iterator, you first need to obtain an Iterator instance. You can use the iterable() method of the Iterable interface to obtain an Iterator instance, or you can directly use the iterator() method of the collection class to obtain an Iterator instance.

List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");

// 使用 Iterable 接口的 iterable() 方法获取 Iterator 实例
Iterator<String> iterator1 = list.iterable();

// 直接使用集合类的 iterator() 方法获取 Iterator 实例
Iterator<String> iterator2 = list.iterator();

After obtaining the Iterator instance, you can use the hasNext() method to check whether there is the next element, you can use the next() method to get the next element, and you can use the remove() method to remove the current element from the collection.

while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
iterator.remove();
}

Use Iterable

To use Iterable, you first need to obtain an Iterable instance. You can create an Iterable instance using an instantiation of the Iterable interface, or you can create an Iterable instance directly using a collection class.

List<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");

// 使用 Iterable 接口的实例化来创建 Iterable 实例
Iterable<String> iterable1 = new ArrayList<>(list);

// 直接使用集合类来创建 Iterable 实例
Iterable<String> iterable2 = list;

After obtaining the Iterable instance, you can use the iterable() method to obtain the Iterator instance, and then you can use the Iterator instance to traverse the elements in the collection.

for (String element : iterable) {
System.out.println(element);
}

The difference between Iterator and Iterable

Iterator and Iterable are two closely related interfaces, but there are some differences between them.

  • Iterator is an iterator that allows the programmer to access the elements in a collection sequentially. An Iterable is an iterable object that generates an Iterator instance to iterate over its elements.
  • Iterator provides some basic methods, including hasNext(), next(), and remove(), for checking whether the next element exists, getting the next element, and removing the current element from the collection. Iterable only provides one basic method, iterable(), which returns an Iterator instance.
  • Iterator can be used directly to traverse elements in a collection. Iterable requires first obtaining an Iterator instance, and then using the Iterator instance to traverse the elements in the collection.

Summarize

Iterator and Iterable are two very important interfaces in the Java collection framework. They provide a standard way to traverse elements in a collection. Iterator provides an iterator for sequential access to the elements of a collection, while Iterable provides a mechanism for creating Iterator instances. Mastering the usage of Iterator and Iterable can help programmers better operate Java collections.

The above is the detailed content of Java Iterator and Iterable from beginner to proficient. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete