Convert an Iterable to Stream in Java Last Updated : 11 Dec, 2018 Comments Improve Suggest changes Like Article Like Report Given an Iterable, the task is to convert it into Stream in Java. Examples: Input: Iterable = [1, 2, 3, 4, 5] Output: {1, 2, 3, 4, 5} Input: Iterable = ['G', 'e', 'e', 'k', 's'] Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterable. Convert the Iterable to Spliterator using Iterable.spliterator() method. Convert the formed Spliterator into Sequential Stream using StreamSupport.stream() method. Return the stream. Below is the implementation of the above approach: Java // Java program to get a Stream // from a given Iterable import java.util.*; import java.util.stream.*; class GFG { // Function to get the Stream public static <T> Stream<T> getStreamFromIterable(Iterable<T> iterable) { // Convert the Iterable to Spliterator Spliterator<T> spliterator = iterable.spliterator(); // Get a Sequential Stream from spliterator return StreamSupport.stream(spliterator, false); } // Driver code public static void main(String[] args) { // Get the Iterator Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5); // Get the Stream from the Iterable Stream<Integer> stream = getStreamFromIterable(iterable); // Print the elements of stream stream.forEach(s -> System.out.println(s)); } } Output: 1 2 3 4 5 Comment More infoAdvertise with us Next Article Convert an Iterable to Stream in Java R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Convert an Iterator to Stream in Java Given an Iterator, the task is to convert it into Stream in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterator. Convert the iterator to Spliterator using Spliterators.split 1 min read Convert an Iterator to a List in Java Given an Iterator, the task is to convert it into List in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Below are the various ways to do so: Naive Approach: Get the Iterator. Create an empty lis 2 min read Convert a Set to Stream in Java Set interface extends Collection interface and Collection has stream() method that returns a sequential stream of the collection. Below given are some examples to understand the implementation in a better way. Example 1 : Converting Integer HashSet to Stream of Integers. Java // Java code for conver 2 min read 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 inte 4 min read Convert Stream to Set in Java Below given are some methods which can be used to convert Stream to Set in Java. Method 1 : Using Collectors Stream collect() method takes elements from a stream and stores them in a collection.collect(Collector.toSet()) collects elements from a stream to a Set. Stream.collect() method can be used t 3 min read Like