Convert an Iterable to Collection in Java
Last Updated : 28 Feb, 2022
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 vice 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:
Java // Below is the program to convert an Iterable // into a Collection using for loop import java.io.*; import java.util.*; class GFG { // function to convert Iterable into Collection public static <T> Collection<T> getCollectionFromIterable(Iterable<T> itr) { // Create an empty Collection to hold the result Collection<T> cltn = new ArrayList<T>(); // Iterate through the iterable to // add each element into the collection for (T t : itr) cltn.add(t); // Return the converted collection return cltn; } public static void main(String[] args) { Iterable<Integer> i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection<Integer> cn = getCollectionFromIterable(i); System.out.println("Collection List : " + cn); } }
Output: Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
- Using Iterable.forEach():
It can be used in Java 8 and above.
Java // Below is the program to convert an Iterable // into a Collection using iterable.forEach import java.io.*; import java.util.*; class GFG { // function to convert Iterable into Collection public static <T> Collection<T> getCollectionFromIterable(Iterable<T> itr) { // Create an empty Collection to hold the result Collection<T> cltn = new ArrayList<T>(); // Use iterable.forEach() to // Iterate through the iterable and // add each element into the collection itr.forEach(cltn::add); // Return the converted collection return cltn; } public static void main(String[] args) { Iterable<Integer> i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection<Integer> cn = getCollectionFromIterable(i); System.out.println("Collection List : " + cn); } }
Output: Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
- Using Iterator: The forEach loop uses Iterator in the background. Hence it can be done explicitly in the following way.
Java // Below is the program to convert an Iterable // into a Collection using Iterator import java.io.*; import java.util.*; class GFG { // function to convert Iterable into Collection public static <T> Collection<T> getCollectionFromIterable(Iterable<T> itr) { // Create an empty Collection to hold the result Collection<T> cltn = new ArrayList<T>(); // Get the iterator at the iterable Iterator<T> iterator = itr.iterator(); // Iterate through the iterable using // iterator to add each element into the collection while (iterator.hasNext()) { cltn.add(iterator.next()); } // Return the converted collection return cltn; } public static void main(String[] args) { Iterable<Integer> i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection<Integer> cn = getCollectionFromIterable(i); System.out.println("Collection List : " + cn); } }
Output: Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
- Java 8 Stream: With the introduction of Stream in Java 8, works like this has become quite easy. To convert iterable to Collection, the iterable is first converted into spliterator. Then with the help of StreamSupport.stream(), the spliterator can be traversed and then collected with the help collect() into collection.
Java // Program to convert an Iterable // into a Collection import java.io.*; import java.util.*; import java.util.stream.*; class GFG { // function to convert Iterable into Collection public static <T> Collection<T> getCollectionFromIterable(Iterable<T> itr) { // Create an empty Collection to hold the result Collection<T> cltn = new ArrayList<T>(); return StreamSupport.stream(itr.spliterator(), false) .collect(Collectors.toList()); } public static void main(String[] args) { Iterable<Integer> i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection<Integer> cn = getCollectionFromIterable(i); System.out.println("Collection List : " + cn); } }
Output: Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
Similar Reads
Convert an Iterable to Stream in Java Given an Iterable, the task is to convert it into Stream in Java. Examples: Input: Iterable = [1, 2, 3, 4, 5] Output: {1, 2, 3, 4, 5} Input: Iterable = ['G', 'e', 'e', 'k', 's'] Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterable. Convert the Iterable to Spliterator using Iterable.spliterat
1 min read
Convert an Iterator to a List in Java Given an Iterator, the task is to convert it into List in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: Naive Approach: Get the Iterator. Create an empty lis
2 min read
Collection Interface in Java The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
Convert Iterator to Iterable in Java Given an Iterator, the task is to convert it into Iterables in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: By overriding the abstract method Iterable.itera
3 min read
Iterator vs Collection in Java Iterator and Collection, both has helped and comforted the programmers at many a times. But their usage and application has a very wide difference. 1. Iterator Declaration public interface Iterator Type Parameters: E - the type of elements returned by this iteratorIterators are used in Collection fr
3 min read