How to convert an iterable to collection in Java
In this tutorial, we are going to learn how to convert an iterable to a collection in Java. An iterator is used to retrieve elements one by one whereas a collection is a group of objects. So, in an iterator, we can only move from one element to others whereas in a collection we can add or remove an element also.
There are various ways to convert an iterable to collection: –
- Using Iterator.
- Using iterator.forEach().
- For loop.
- Using Stream in Java 8.
Using Iterator In Java
In this first, we will create a function that has an iterable as an argument after that we will initialize an empty collection. And will iterate through the iterator by adding each element in the iterable to the empty collection after that we will return the collection.
Code: –
import java.io.*; import java.util.*; public class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> it) { Collection<T> arr = new ArrayList<T>();//Empty Colection Iterator<T> iterator = it.iterator(); //For iterating through the it while (iterator.hasNext()) { arr.add(iterator.next()); } return arr; } public static void main(String[] args) { Iterable<String> irr = Arrays.asList("ASD", "QWE", "ZXC"); System.out.println("Iterable : " + irr); Collection<String> lst = iterableToCollection(irr); System.out.println("Collection : " + lst); } }
Output: –
Iterable : [ASD, QWE, ZXC] Collection : [ASD, QWE, ZXC]
Using iterator.forEach()
The basic idea is the same as above. So, we are just changing the way to iterate in this we will use iterator.forEach().
Code: –
import java.io.*; import java.util.*; public class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> it) { Collection<T> arr = new ArrayList<T>();//Empty Colection it.forEach(arr::add);//forEach function return arr; } public static void main(String[] args) { Iterable<String> irr = Arrays.asList("ASD", "QWE", "ZXC"); System.out.println("Iterable : " + irr); Collection<String> lst = iterableToCollection(irr); System.out.println("Collection : " + lst); } }
Output: –
Iterable : [ASD, QWE, ZXC] Collection : [ASD, QWE, ZXC]
Using For Loop
So, in this we will use For loop.
Code: –
import java.io.*; import java.util.*; public class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> it) { Collection<T> arr = new ArrayList<T>();//Empty Colection for(T t:it){ arr.add(t); } return arr; } public static void main(String[] args) { Iterable<String> irr = Arrays.asList("ASD", "QWE", "ZXC"); System.out.println("Iterable : " + irr); Collection<String> lst = iterableToCollection(irr); System.out.println("Collection : " + lst); } }
Output: –
Iterable : [ASD, QWE, ZXC] Collection : [ASD, QWE, ZXC]
Using Stream in Java 8
In Java 8 a new feature called Stream was added which made this conversion easy. So, in this process, we convert the iterable to spliterator. After that using StreamSupport.stream() the stream is traversed and is collected to a collection using collect().
Code: –
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static <T> Collection<T> iterableToCollection(Iterable<T> it) { Collection<T> arr = new ArrayList<T>();//Empty Colection return StreamSupport.stream(it.spliterator(), false) .collect(Collectors.toList()); } public static void main(String[] args) { Iterable<String> irr = Arrays.asList("ASD", "QWE", "ZXC"); System.out.println("Iterable : " + irr); Collection<String> lst = iterableToCollection(irr); System.out.println("Collection : " + lst); } }
Output: –
Iterable : [ASD, QWE, ZXC] Collection : [ASD, QWE, ZXC]
These are the different ways by which we can convert iterable to a Collection in Java.
Also Read: –
Leave a Reply