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:
-
Using For loop
// Below is the program to convert an Iterable// into a Collection using for loopimportjava.io.*;importjava.util.*;classGFG {// function to convert Iterable into Collectionpublicstatic<T> Collection<T>getCollectionFromIteralbe(Iterable<T> itr){// Create an empty Collection to hold the resultCollection<T> cltn =newArrayList<T>();// Iterate through the iterable to// add each element into the collectionfor(T t : itr)cltn.add(t);// Return the converted collectionreturncltn;}publicstaticvoidmain(String[] args){Iterable<Integer> i = Arrays.asList(1,2,3,4);System.out.println("Iterable List : "+ i);Collection<Integer> cn = getCollectionFromIteralbe(i);System.out.println("Collection List : "+ cn);}}chevron_rightfilter_noneOutput:Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
-
Using Iterable.forEach():
It can be used in Java 8 and above.// Below is the program to convert an Iterable// into a Collection using iterable.forEachimportjava.io.*;importjava.util.*;classGFG {// function to convert Iterable into Collectionpublicstatic<T> Collection<T>getCollectionFromIteralbe(Iterable<T> itr){// Create an empty Collection to hold the resultCollection<T> cltn =newArrayList<T>();// Use iterable.forEach() to// Iterate through the iterable and// add each element into the collectionitr.forEach(cltn::add);// Return the converted collectionreturncltn;}publicstaticvoidmain(String[] args){Iterable<Integer> i = Arrays.asList(1,2,3,4);System.out.println("Iterable List : "+ i);Collection<Integer> cn = getCollectionFromIteralbe(i);System.out.println("Collection List : "+ cn);}}chevron_rightfilter_noneOutput: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.
// Below is the program to convert an Iterable// into a Collection using Iteratorimportjava.io.*;importjava.util.*;classGFG {// function to convert Iterable into Collectionpublicstatic<T> Collection<T>getCollectionFromIteralbe(Iterable<T> itr){// Create an empty Collection to hold the resultCollection<T> cltn =newArrayList<T>();// Get the iterator at the iterableIterator<T> iterator = itr.iterator();// Iterate through the iterable using// iterator to add each element into the collectionwhile(iterator.hasNext()) {cltn.add(iterator.next());}// Return the converted collectionreturncltn;}publicstaticvoidmain(String[] args){Iterable<Integer> i = Arrays.asList(1,2,3,4);System.out.println("Iterable List : "+ i);Collection<Integer> cn = getCollectionFromIteralbe(i);System.out.println("Collection List : "+ cn);}}chevron_rightfilter_noneOutput:Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
-
Using For loop
- 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.
// Program to convert an Iterable// into a Collectionimportjava.io.*;importjava.util.*;importjava.util.stream.*;classGFG {// function to convert Iterable into Collectionpublicstatic<T> Collection<T>getCollectionFromIteralbe(Iterable<T> itr){// Create an empty Collection to hold the resultCollection<T> cltn =newArrayList<T>();returnStreamSupport.stream(itr.spliterator(),false).collect(Collectors.toList());}publicstaticvoidmain(String[] args){Iterable<Integer> i = Arrays.asList(1,2,3,4);System.out.println("Iterable List : "+ i);Collection<Integer> cn = getCollectionFromIteralbe(i);System.out.println("Collection List : "+ cn);}}chevron_rightfilter_noneOutput:Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
Recommended Posts:
- Convert Iterator to Iterable in Java
- Convert an Iterable to Stream in Java
- Java | Implementing Iterator and Iterable Interface
- Iterator vs Collection in Java
- Garbage Collection in Java
- Difference between Arrays and Collection in Java
- Collection add() method in Java with Examples
- Collection contains() method in Java with Examples
- Sorting collection of String and StringBuffer in Java
- Stack addAll(Collection) method in Java with Example
- Stack addAll(int, Collection) method in Java with Example
- Output of Java programs | Set 10 (Garbage Collection)
- Collection isEmpty() method in Java with Examples
- Collection addAll() method in Java with Examples
- Collection clear() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



