Iterable and Collection have served to be of great use in Java. Iterators are used in Collection framework in Java to retrieve elements one by one and a Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit.
But at certain times, it is required to switch from iterable to collection and vie versa. For more details on difference between Iterable and Collection, please refer to the post Iterator vs Collection in Java.
The conversion of Iterable to Collection can be carried out in following ways:
-
Creating a utility function: Creating a utility function means creating a function that converts the iterable to a collection by explicitly taking each item into account. This also can be done in many ways as explained below:
- Please complete the security check to access www.techiedelight.com
- Why do I have to complete a CAPTCHA?
- What can I do to prevent this in the future?
- I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:
- 1. Overview
- 2. Iterable and Iterator
- 3. Using Plain Java
- 3.1. Iterable to Collection
- 3.2. Iterator to Collection
- 3.3. Using a For-Loop
- 4. Using Guava
- 5. Using Apache Commons
- 6. Conclusion
Please complete the security check to access www.techiedelight.com
Why do I have to complete a CAPTCHA?
Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.
What can I do to prevent this in the future?
If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.
If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.
Another way to prevent getting this page in the future is to use Privacy Pass. You may need to download version 2.0 now from the Chrome Web Store.
Cloudflare Ray ID: 538c43b65b9bc765 • Your IP : 78.85.5.224 • Performance & security by Cloudflare
Last modified: July 19, 2019
I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:
1. Overview
In this tutorial, we’ll explore different ways to convert an Iterable to a Collection in Java.
We’ll start with plain Java solutions, then have a look at the options that the Guava and Apache Commons libraries also provide.
2. Iterable and Iterator
First, we’ll define our Iterable:
We’ll also define a simple Iterator – to highlight the difference between converting Iterable to Collection and Iterator to Collection:
3. Using Plain Java
3.1. Iterable to Collection
We can use the Java 8 forEach() method to add all elements to the List:
Or use the Spliterator class to convert our Iterable to Stream then to Collection:
3.2. Iterator to Collection
On the other hand, instead of using forEach(), we’ll use forEachRemaining() with Iterator:
We can also create a Spliterator from our Iterator then use it to convert Iterator to Stream:
3.3. Using a 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:
On the other hand, we’ll use hasNext() and next() with the Iterator:
4. Using Guava
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:
We can create a new List from Iterable or Iterator using Lists.newArrayList():
Or we can use ImmutableList.copyOf():
5. Using Apache Commons
Finally, we’ll use Apache Commons IterableUtils to create a List from Iterable:
Similarly, we’ll use IteratorUtils to create a List from our Iterator:
6. Conclusion
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.
As always, the full source code is available over on GitHub.