How to convert an Iterable to Collection in Java.

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:

  1. Creating a utility function: Creating a utility function means creating a function that transforms an iterable object into a collection by explicitly considering each item. This can also be done in a variety of ways, as described below:
  • Using For loop
// 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]
  • 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.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]
  • Using Iterator: The forEach loop uses an Iterator in the background. Therefore, it can be done explicitly in the following way.
// 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]

 

Submit Your Programming Assignment Details