1. Overview
1.概述
In this tutorial, we’ll explore different ways to convert an Iterable to a Collection in Java.
在本教程中,我们将探讨在Java中把Iterable转换为Collection的不同方法。
We’ll start with plain Java solutions, then have a look at the options that the Guava and Apache Commons libraries also provide.
我们将从普通的Java解决方案开始,然后看看Guava和Apache Commons库也提供的选项。
2. Iterable and Iterator
2.Iterable和Iterator
First, we’ll define our Iterable:
首先,我们将定义我们的Iterable。
Iterable<String> iterable = Arrays.asList("john", "tom", "jane");
We’ll also define a simple Iterator – to highlight the difference between converting Iterable to Collection and Iterator to Collection:
我们还将定义一个简单的Iterator–以强调将Iterable转换为Collection和Iterator转换为Collection之间的区别。
Iterator<String> iterator = iterable.iterator();
3. Using Plain Java
3.使用普通的Java
3.1. Iterable to Collection
3.1.Iterable到Collection
We can use the Java 8 forEach() method to add all elements to the List:
我们可以使用Java 8的forEach()方法,将所有元素添加到List。
@Test
public void whenConvertIterableToListUsingJava8_thenSuccess() {
List<String> result = new ArrayList<String>();
iterable.forEach(result::add);
assertThat(result, contains("john", "tom", "jane"));
}
Or use the Spliterator class to convert our Iterable to Stream then to Collection:
或者使用Spliterator类将我们的Iterable转换成Stream,然后再转换成Collection。
List<String> result =
StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
3.2. Iterator to Collection
3.2.Iterator到Collection
On the other hand, instead of using forEach(), we’ll use forEachRemaining() with Iterator:
另一方面,我们不使用forEach(),而是使用forEachRemaining()与Iterator。
@Test
public void whenConvertIteratorToListUsingJava8_thenSuccess() {
List<String> result = new ArrayList<String>();
iterator.forEachRemaining(result::add);
assertThat(result, contains("john", "tom", "jane"));
}
We can also create a Spliterator from our Iterator then use it to convert Iterator to Stream:
我们也可以从我们的Iterator创建一个Spliterator,然后用它来将Iterator转换为Stream。
List<String> result =
StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
.collect(Collectors.toList());
3.3. Using a For-Loop
3.3.使用For-Loop
Let’s also have a look at a solution that uses a very simple for-loop to convert our Iterable to a List:
我们也来看看一个解决方案,它使用一个非常简单的for-loop来将我们的Iterable转换为List。
@Test
public void whenConvertIterableToListUsingJava_thenSuccess() {
List<String> result = new ArrayList<String>();
for (String str : iterable) {
result.add(str);
}
assertThat(result, contains("john", "tom", "jane"));
}
On the other hand, we’ll use hasNext() and next() with the Iterator:
另一方面,我们将使用hasNext()和next()与Iterator。
@Test
public void whenConvertIteratorToListUsingJava_thenSuccess() {
List<String> result = new ArrayList<String>();
while (iterator.hasNext()) {
result.add(iterator.next());
}
assertThat(result, contains("john", "tom", "jane"));
}
4. Using Guava
4.使用番石榴
There are also a few libraries available that provide convenient methods to help us achieve this.
也有一些库可以提供方便的方法来帮助我们实现这一目标。
Let’s see how we can use Guava to convert from an Iterable to a List:
让我们看看如何使用Guava来从Iterable转换到List。
We can create a new List from Iterable or Iterator using Lists.newArrayList():
我们可以使用Lists.newArrayList()从Iterable或Iterator创建一个新的List:。
List<String> result = Lists.newArrayList(iterable);
Or we can use ImmutableList.copyOf():
或者我们可以使用ImmutableList.copyOf()。
List<String> result = ImmutableList.copyOf(iterable);
5. Using Apache Commons
5.使用Apache Commons
Finally, we’ll use Apache Commons IterableUtils to create a List from Iterable:
最后,我们将使用Apache CommonsIterableUtils,从Iterable创建一个List:
List<String> result = IterableUtils.toList(iterable);
Similarly, we’ll use IteratorUtils to create a List from our Iterator:
同样地,我们将使用IteratorUtils从我们的Iterator创建一个List。
List<String> result = IteratorUtils.toList(iterator);
6. Conclusion
6.结论
In this short article, we learned how to convert an Iterable and Iterator to a Collection using Java. We explored different ways using plain Java, and two external libraries: Guava and Apache Commons.
在这篇短文中,我们学习了如何使用Java将Iterable和Iterator转换成Collection。我们探索了使用普通Java和两个外部库的不同方法。Guava和Apache Commons。
As always, the full source code is available over on GitHub.
一如既往,完整的源代码可以在GitHub上获得。