Convert Iterator to Iterable in Java - GeeksforGeeks

10 Nov, 2021
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  import java.util.*;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return new Iterable<T>() {            @Override            public Iterator<T> iterator()            {                return iterator;            }        };    }      // Driver code    public static void main(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

Get the Iterator.Override the Iterable.iterator() method and get the iterable.Return the Iterable.
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  import java.util.*;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return new Iterable<T>() {            @Override            public Iterator<T> iterator()            {                return iterator;            }        };    }      // Driver code    public static void main(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);    }}
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  import java.util.*;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return () -> iterator;    }      // Driver code    public static void main(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

Get the Iterator.Convert the iterator to iterable using Lambda Expression.Return the Iterable.
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  import java.util.*;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return () -> iterator;    }      // Driver code    public static void main(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);    }}
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  import java.util.*;import java.util.stream.Collectors;import java.util.stream.StreamSupport;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return StreamSupport              // 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    public static void main(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

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.
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  import java.util.*;import java.util.stream.Collectors;import java.util.stream.StreamSupport;  class GFG {      // Function to get the Spliterator    public static <T> Iterable<T>    getIterableFromIterator(Iterator<T> iterator)    {        return StreamSupport              // 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    public static void main(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);    }}
1
2
3
4
5

sumitgumber28
Java - util package
Java-Iterable
Java-Iterator
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java