
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Convert an Iterable to Collection in Java
Let’s say the following is our Iterable −
Iterable<Integer> i = Arrays.asList(50, 100, 150, 200, 250, 300, 500, 800, 1000);
Now, create a Collection −
Collection<Integer> c = convertIterable(i);
Above, we have a custom method convertIterable() for conversion. Following is the method −
public static <T> Collection<T> convertIterable(Iterable<T> iterable) { if (iterable instanceof List) { return (List<T>) iterable; } return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); }
Example
Following is the program to convert an Iterable to Collection in Java −
import java.util.*; import java.util.stream.*; public class Demo { public static <T> Collection<T> convertIterable(Iterable<T> iterable) { if (iterable instanceof List) { return (List<T>) iterable; } return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); } public static void main(String[] args) { Iterable<Integer> i = Arrays.asList(50, 100, 150, 200, 250, 300, 500, 800, 1000); Collection<Integer> c = convertIterable(i); System.out.println("Collection (Iterable to Collection) = "+c); } }
Output
Collection (Iterable to Collection) = [50, 100, 150, 200, 250, 300, 500, 800, 1000]
- Related Articles
- Convert an Iterable to Stream in Java
- Convert Iterator to Iterable in Java
- How to convert Java Array to Iterable?
- Java Program to Convert Collection into Array
- Java Program to Convert Array into Collection
- How to convert Java Array/Collection to JSON array?
- How to convert a list collection into a dictionary in Java?
- Invoke convertToCapped and convert an existing collection to capped in MongoDB
- Convert Collection to Capped in MongoDB
- How can we implement a custom iterable in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- Convert an Iterator to Stream in Java
- Convert an ArrayList to HashSet in Java
- Convert LinkedList to an array in Java
- Create and demonstrate an immutable collection in Java

Advertisements