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():
    1. Get the Iterator.
    2. Override the Iterable.iterator() method and get the iterable.
    3. Return the Iterable.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:



    1
    2
    3
    4
    5
    
  • Using Java 8 lambda expression:
    1. Get the Iterator.
    2. Convert the iterator to iterable using Lambda Expression.
    3. Return the Iterable.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    1
    2
    3
    4
    5
    
  • Using Spliterators:
    1. Get the Iterator.
    2. Convert the iterator to Splietrator using Spliterators.spliteratorUnknownSize() method.
    3. Convert the Spliterator into Sequential Stream using StreamSupport.stream() method.
    4. Collect the elements of the Sequential Stream as an Iterable using collect() method.
    5. Return the Iterable.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    1
    2
    3
    4
    5
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.