Convert an Iterable to a Collection in Java
This post will discuss how to convert an Iterable to a Collection in Java.
1. Naive solution
A naive solution is to write our own utility method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { Collection<T> collection = new ArrayList<T>(); for (T e: iterable) { collection.add(e); } return collection; } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
We know that for-each loop uses an iterator behind the scenes. We can also use the iterators, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { Collection<T> collection = new ArrayList<>(); Iterator<T> iterator = iterable.iterator(); while (iterator.hasNext()) { collection.add(iterator.next()); } return collection; } |
In Java 8 and above, we can do something like:
|
1 2 3 4 5 6 7 |
public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { Collection<T> collection = new ArrayList<>(); iterable.forEach(collection::add); return collection; } |
2. Using Java 8 Stream
Another solution is to use streams in Java 8 and above. The idea is to convert the iterable to Spliterator first. Then we can easily get a stream from spliterator using the StreamSupport.stream() method. Finally, we collect values from the Stream into a List using Collectors.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import java.util.stream.StreamSupport; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { // check if iterable is an instance of a list if (iterable instanceof List) { return (List<T>) iterable; } // convert iterable to spliterator, get the stream from spliterator, // and collect values into an Iterable (say list) return StreamSupport.stream(iterable.spliterator(), false) .collect(Collectors.toList()); } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
3. Using Guava Library
We can also use Lists.newArrayList() or ImmutableList.copyOf() methods provided by Guava that takes an iterable and returns a mutable or immutable list, respectively.
To get a set instead, we can use Sets.newHashSet() or ImmutableSet.copyOf() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.Collection; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { return Lists.newArrayList(iterable); } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
We can also change the iterable to be a FluentIterable and then make a call to FluentIterable.toList(). This can be used with Java 7 or before (before the introduction of the stream library in Java 8 and above).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.collect.FluentIterable; import java.util.Arrays; import java.util.Collection; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { return FluentIterable.from(iterable) .toList(); } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
4. Using Apache Commons Collections
Apache Commons Collections IteratorUtils class also provides many static utility methods and decorators for Iterator instances. We can use the toList() method that takes an iterator and returns a list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.IteratorUtils; import java.util.Arrays; import java.util.Collection; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { return IteratorUtils.toList(iterable.iterator()); } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
Another approach is to use the CollectionUtils.addAll() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import org.apache.commons.collections4.CollectionUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> iterable) { Collection<T> collection = new ArrayList<>(); CollectionUtils.addAll(collection, iterable.iterator()); return collection; } // Program to convert an Iterable to a Collection in Java public static void main(String[] args) { Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); Collection<Integer> collection = iterableToCollection(iterable); System.out.println(collection); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about converting an Iterable to a Collection in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)