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 IteratorÂÂimportjava.util.*;ÂÂclassGFG {   Â// Function to get the Spliterator   Âpublicstatic<T> Iterable<T>   ÂgetIterableFromIterator(Iterator<T> iterator)   Â{       ÂreturnnewIterable<T>() {           Â@Override           ÂpublicIterator<T> iterator()           Â{               Âreturniterator;           Â}       Â};   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Iterator       ÂIterator<Integer>           Âiterator = Arrays.asList(1,2,3,4,5)                          Â.iterator();       Â// Get the Iterable from the Iterator       ÂIterable<Integer>           Âiterable = getIterableFromIterator(iterator);       Â// Print the elements of Iterable       Âiterable.forEach(System.out::println);   Â}}Output: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 IteratorÂÂimportjava.util.*;ÂÂclassGFG {   Â// Function to get the Spliterator   Âpublicstatic<T> Iterable<T>   ÂgetIterableFromIterator(Iterator<T> iterator)   Â{       Âreturn() -> iterator;   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Iterator       ÂIterator<Integer>           Âiterator = Arrays.asList(1,2,3,4,5)                          Â.iterator();       Â// Get the Iterable from the Iterator       ÂIterable<Integer>           Âiterable = getIterableFromIterator(iterator);       Â// Print the elements of Iterable       Âiterable.forEach(System.out::println);   Â}}Output:1 2 3 4 5
- Using Spliterators:
- Get the Iterator.
- Convert the iterator to Spliterator 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 IteratorÂÂimportjava.util.*;importjava.util.stream.Collectors;importjava.util.stream.StreamSupport;ÂÂclassGFG {   Â// Function to get the Spliterator   Âpublicstatic<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 code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Iterator       ÂIterator<Integer>           Âiterator = Arrays.asList(1,2,3,4,5)                          Â.iterator();       Â// Get the Iterable from the Iterator       ÂIterable<Integer>           Âiterable = getIterableFromIterator(iterator);       Â// Print the elements of Iterable       Âiterable.forEach(System.out::println);   Â}}Output:1 2 3 4 5
