
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert an Iterable to Collection in Java
In Java, we can store objects within other objects. A collection is an object that stores other objects. Some examples of collections are ArrayList, HashSet.
Iterable is an Interface that is used for iterating over a Collection of objects. It is part of the Java Collections Framework. The Iterable interface is implemented by all the collection classes in Java. The Iterable interface has a method named iterator() which returns an Iterator object.
In this article, we will learn how to convert an Iterable to a Collection in Java using multiple ways. Those are -
- Using for-each loop
- Using Stream API
- Using Iterator
Let's understand each of them in detail.
Convert an Iterable to a Collection using a for-each loop
In this method, we will use a for-each loop to iterate over the Iterable and add each element to a new Collection.
import java.util.*; import java.io.*; public class IterableToCollection { public static void main(String[] args) { // Create an Iterable object Iterable<String> iterable = Arrays.asList("A", "B", "C", "D", "E"); // Create a Collection object Collection<String> collection = new ArrayList<>(); // Iterate over the Iterable and add each element to the Collection for(String element : iterable) { collection.add(element); } // Print the Collection System.out.println("Collection: " + collection); } }
Output
The output of the above code will be:
Collection: [A, B, C, D, E]
Convert an Iterable to a Collection using Stream API
Here, we will use the Stream API to convert an Iterable to a Collection. The Stream API is a new abstraction introduced in Java 8. It allows us to process sequences of elements in a functional style.
import java.util.*; import java.io.*; import java.util.stream.*; public class IterableToCollection { public static void main(String[] args) { // Create an Iterable object Iterable<String> iterable = Arrays.asList("A", "B", "C", "D", "E"); // Convert the Iterable to a Collection using Stream API Collection<String> collection = StreamSupport.stream(iterable.spliterator(), false) .collect(Collectors.toList()); // Print the Collection System.out.println("Collection: " + collection); } }
Output
The output of the above code will be -
Collection: [A, B, C, D, E]
Convert an Iterable to a Collection using Iterator
We can also use an Iterator to iterate over the Iterable and add each element to a new Collection.
import java.util.*; import java.io.*; public class IterableToCollection { public static void main(String[] args) { // Create an Iterable object Iterable<String> iterable = Arrays.asList("A", "B", "C", "D", "E"); // Create a Collection object Collection<String> collection = new ArrayList<>(); // Create an Iterator object Iterator<String> iterator = iterable.iterator(); // Iterate over the Iterable and add each element to the Collection while(iterator.hasNext()) { collection.add(iterator.next()); } // Print the Collection System.out.println("Collection: " + collection); } }
Output
The output of the above code will be -
Collection: [A, B, C, D, E]