Convert Iterator to Iterable in Java
Given an Iterator, the task is to convert it into Iterables 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:
- By overriding the abstract method Iterable.iterator():
- Get the Iterator.
- Override the Iterable.iterator() method and get the iterable.
- Return the Iterable.
Below is the implementation of the above approach:
// Java program to get a Iterable// from a given Iteratorimportjava.util.*;classGFG {// Function to get the Spliteratorpublicstatic<T> Iterable<T>getIterableFromIterator(Iterator<T> iterator){returnnewIterable<T>() {@OverridepublicIterator<T> iterator(){returniterator;}};}// Driver codepublicstaticvoidmain(String[] args){// Get the IteratorIterator<Integer>iterator = Arrays.asList(1,2,3,4,5).iterator();// Get the Iterable from the IteratorIterable<Integer>iterable = getIterableFromIterator(iterator);// Print the elements of Iterableiterable.forEach(System.out::println);}}chevron_rightfilter_noneOutput:1 2 3 4 5
- Using Java 8 lambda expression:
- Get the Iterator.
- Convert the iterator to iterable using Lambda Expression.
- Return the Iterable.
Below is the implementation of the above approach:
// Java program to get an Iterable// from a given Iteratorimportjava.util.*;classGFG {// Function to get the Spliteratorpublicstatic<T> Iterable<T>getIterableFromIterator(Iterator<T> iterator){return() -> iterator;}// Driver codepublicstaticvoidmain(String[] args){// Get the IteratorIterator<Integer>iterator = Arrays.asList(1,2,3,4,5).iterator();// Get the Iterable from the IteratorIterable<Integer>iterable = getIterableFromIterator(iterator);// Print the elements of Iterableiterable.forEach(System.out::println);}}chevron_rightfilter_noneOutput:1 2 3 4 5
- Using Spliterators:
- Get the Iterator.
- Convert the iterator to Splietrator using Spliterators.spliteratorUnknownSize() method.
- Convert the Spliterator into Sequential Stream using StreamSupport.stream() method.
- Collect the elements of the Sequential Stream as an Iterable using collect() method.
- Return the Iterable.
Below is the implementation of the above approach:
// Java program to get a Iterable// from a given Iteratorimportjava.util.*;importjava.util.stream.Collectors;importjava.util.stream.StreamSupport;classGFG {// Function to get the Spliteratorpublicstatic<T> Iterable<T>getIterableFromIterator(Iterator<T> iterator){returnStreamSupport// Convert the iterator into a Spliterator// and then into a sequential stream.stream(Spliterators.spliteratorUnknownSize(iterator,0),false)// Convert the stream to an iterable.collect(Collectors.toList());}// Driver codepublicstaticvoidmain(String[] args){// Get the IteratorIterator<Integer>iterator = Arrays.asList(1,2,3,4,5).iterator();// Get the Iterable from the IteratorIterable<Integer>iterable = getIterableFromIterator(iterator);// Print the elements of Iterableiterable.forEach(System.out::println);}}chevron_rightfilter_noneOutput:1 2 3 4 5
Recommended Posts:
- Java | Implementing Iterator and Iterable Interface
- Convert an Iterable to Stream in Java
- Convert an Iterable to Collection in Java
- Convert an Iterator to Stream in Java
- Convert an Iterator to a List in Java
- Java Program to Convert Iterator to Spliterator
- Iterable forEach() method in Java with Examples
- How to use Iterator in Java?
- IntStream iterator() in Java
- LongStream iterator() in Java
- DoubleStream iterator() in Java
- Iterator vs Foreach In Java
- Iterator vs Collection in Java
- NavigableSet iterator() method in Java
- CopyOnWriteArrayList iterator() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



