Iterable and Collection are very useful in Java. Java's Collection framework uses iterators to retrieve elements one by one. A Collection is a group of single objects represented as a single unit. Java provides a collection framework, which defines several classes and interfaces to represent a group of objects as a unit.
But at some point, you need to go from iterable to the collection, and vice versa. For more details on the 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 the following ways:
// 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 Collection getCollectionFromIteralbe(Iterable itr) { // Create an empty Collection to hold the result Collection cltn = new ArrayList(); // 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 i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection cn = getCollectionFromIteralbe(i); System.out.println("Collection List : " + cn); } }
Output:
Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
// 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 Collection getCollectionFromIteralbe(Iterable itr) { // Create an empty Collection to hold the result Collection cltn = new ArrayList(); // 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 i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection cn = getCollectionFromIteralbe(i); System.out.println("Collection List : " + cn); } }
Output:
Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
// 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 Collection getCollectionFromIteralbe(Iterable itr) { // Create an empty Collection to hold the result Collection cltn = new ArrayList(); // Get the iterator at the iterable Iterator 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 i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection cn = getCollectionFromIteralbe(i); System.out.println("Collection List : " + cn); } }
Output:
Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]
2. Java 8 Stream:
With the introduction of Stream in Java 8, this kind of work has become very easy. To convert iterable to Collection, first convert iterable to spliterator. Then with the help of StreamSupport.stream(), you can traverse the spliterator, and then collect it into the collection through help collect().
// 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 Collection getCollectionFromIteralbe(Iterable itr) { // Create an empty Collection to hold the result Collection cltn = new ArrayList(); return StreamSupport.stream(itr.spliterator(), false) .collect(Collectors.toList()); } public static void main(String[] args) { Iterable i = Arrays.asList(1, 2, 3, 4); System.out.println("Iterable List : " + i); Collection cn = getCollectionFromIteralbe(i); System.out.println("Collection List : " + cn); } }
Output:
Iterable List : [1, 2, 3, 4] Collection List : [1, 2, 3, 4]