1. Overview
1.概述
In this short tutorial, we’ll learn how to convert an Iterator to a List in Java. We’ll cover a few examples using a while loop, Java 8, and a few common libraries.
在这个简短的教程中,我们将学习如何在Java中把Iterator转换成List。我们将介绍几个使用while循环、Java 8和一些常用库的例子。
We’ll use an Iterator with Integers for all our examples:
我们将在所有的例子中使用一个带有Integer的Iterator。
Iterator<Integer> iterator = Arrays.asList(1, 2, 3).iterator();
2. Using a While Loop
2.使用While循环
Let’s begin with the approach traditionally used before Java 8. We’ll convert the Iterator to a List using a while loop:
让我们从Java 8之前传统使用的方法开始。我们将将Iterator转换为List,使用 while循环。
List<Integer> actualList = new ArrayList<Integer>();
while (iterator.hasNext()) {
actualList.add(iterator.next());
}
assertThat(actualList, containsInAnyOrder(1, 2, 3));
3. Using Java 8 Iterator.forEachRemaining
3.使用Java 8 Iterator.forEachRemaining
In Java 8 and later, we can use the Iterator‘s forEachRemaining() method to build our List. We’ll pass the add() method of the List interface as a method reference:
在 Java 8 及更高版本中,我们可以使用 Iterator 的 forEachRemaining() 方法来构建我们的 List。我们将把List接口的add()方法作为方法引用传递。
List<Integer> actualList = new ArrayList<Integer>();
iterator.forEachRemaining(actualList::add);
assertThat(actualList, containsInAnyOrder(1, 2, 3));
4. Using the Java 8 Streams API
4.使用Java 8 Streams API
Next, we’ll use the Java 8 Streams API to convert the Iterator to a List. In order to use the Stream API, we need to first convert the Iterator to an Iterable. We can do this using Java 8 Lambda expressions:
接下来,我们将使用Java 8 Streams API来将Iterator转换为List。为了使用Stream API,我们需要首先将Iterator转换为Iterable/em>。我们可以使用Java 8的Lambda表达式来做到这一点。
Iterable<Integer> iterable = () -> iterator;
Now, we can use the StreamSupport class’ stream() and collect() methods to build the List:
现在,我们可以使用StreamSupport类的stream()和collect()方法来构建List。
List<Integer> actualList = StreamSupport
.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
assertThat(actualList, containsInAnyOrder(1, 2, 3));
5. Using Guava
5.使用番石榴
The Guava library from Google provides options to create both a mutable and immutable Lists, so we’ll see both approaches.
来自Google的Guava库提供了创建可变和可变Lists的选项,所以我们将看到两种方法。
Let’s first create an immutable List using ImmutableList.copyOf() method:
让我们首先使用ImmutableList.copyOf()方法创建一个不可变的List。
List<Integer> actualList = ImmutableList.copyOf(iterator);
assertThat(actualList, containsInAnyOrder(1, 2, 3));
Now, let’s create a mutable List using Lists.newArrayList() method:
现在,让我们使用Lists.newArrayList()方法创建一个可变的List。
List<Integer> actualList = Lists.newArrayList(iterator);
assertThat(actualList, containsInAnyOrder(1, 2, 3));
6. Using Apache Commons
6.使用Apache Commons
The Apache Commons Collections library provides options to work on a List. We’ll use IteratorUtils to do the conversion:
Apache Commons Collections库提供了用于处理List的选项。我们将使用IteratorUtils来进行转换。
List<Integer> actualList = IteratorUtils.toList(iterator);
assertThat(actualList, containsInAnyOrder(1, 2, 3));
7. Conclusion
7.结语
In this article, we’ve covered a few options for converting an Iterator to a List. While there are a few other ways of achieving this, we’ve covered several commonly used options.
在这篇文章中,我们介绍了将Iterator转换为List的几个选项。虽然还有一些其他的实现方式,但我们已经涵盖了几个常用的选项。
The implementation of all these examples and code snippets can be found over on GitHub.
所有这些例子和代码片断的实现都可以在GitHub上找到over。