Open In App

Convert an Iterable to Collection in Java

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:
 




// 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]

 




// 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]

 




// 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]

 




// 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]

 


Article Tags :