Home » Java programming language

How to convert an Iterator into Iterable in Java?

Converting an Iterator into Iterable: Here, we are going to learn how to convert an Iterator into Iterable in Java programming language?
Submitted by Preeti Jain, on September 02, 2019

There are various ways to convert an Iterator into Iterable,

  1. Overriding an abstract method iterator()
  2. Lambda expression in Java 8
  3. By using Spliterators

1) Overriding an abstract method iterator()

We will see in various steps to override an abstract method iterator(),

  • We will get the Iterator object by using iterator() method of Collection.
  • We will get the Iterable object by overriding an iterator() method.
  • We will define another method and in this method, we will pass the Iterator object and override iterator() method and then return Iterator object.

Example:

// Java program to demonstrate the example  of overriding 
// an abstract method iterator() to convert Iterator into Iterable

import java.util.*;

public class ConvertIteratorToIterable {
    // This is a user defined method to convert Iterator into Iterable
    public static Iterable convertIterableFromIterator(Iterator iterator) {
        return new Iterable() {
            // Overriding an abstract method iterator()
            public Iterator iterator() {
                return iterator;
            }
        };
    }

    public static void main(String[] args) {
        // Instantiating Iterator Object
        Iterator iterate = Arrays.asList(10, 20, 30, 40).iterator();

        // Now, we will get the Iterable from Iterator
        Iterable iterable = convertIterableFromIterator(iterate);

        // Display converted Iterable elements
        iterable.forEach(System.out::println);
    }
}

Output

10
20
30
40

2) Lambda expression in Java 8

We will see in various steps and the steps are given below:

  • In the first step, Get the Iterator.
  • In the second step, we will convert Iterator into Iterable without overriding an abstract method iterator().
  • In the third step, we will return Iterable from the method convertIteratorToIterable() and passed the Iterator object and get Iterable.

Example:

// Java program to demonstrate the example of  
// Lambda expression in Java 8 to convert Iterator into Iterable

import java.util.*;

public class ConvertIteratorToIterable {
    // This is a user defined method to convert Iterator into Iterable
    public static Iterable convertIterableFromIterator(Iterator iterator) {
        return () -> iterator;
    }

    public static void main(String[] args) {
        // Instantiating Iterator Object
        Iterator iterate = Arrays.asList(10, 20, 30, 40).iterator();

        // Now, we will get the Iterable from Iterator
        Iterable iterable = convertIterableFromIterator(iterate);

        // Display converted Iterable elements
        iterable.forEach(System.out::println);
    }
}

Output

10
20
30
40

3) By using Spliterators

We will see in few steps and the steps are given below:

  • To get the Iterator and convert the Iterator into Spliterator by using Spliterators.spliteratorUnknownSize() method and by using this method we will take iterator object, size, a Boolean value.
  • Now, our data is ready in Spliterator and again we will convert Spliterator into Stream by using stream() method.
  • Now, our data is ready in Stream and again we will convert Stream to Iterable by using the collect() method.
  • At last, we will return the Iterable.

Example:

// Java program to demonstrate the example of Spliterators to 
// convert an Iterator into Iterable

import java.util.*;
import java.util.stream.*;

public class ConvertIteratorToIterable {
    // This is a user defined method to convert Iterator into Iterable
    public static < T > Iterable < T > convertIterableFromIterator(Iterator < T > iterator) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).collect(Collectors.toList());

    }

    public static void main(String[] args) {
        // Instantiating Iterator Object
        Iterator < Double > iterate = Arrays.asList(10.0, 20.0, 30.0, 40.0).iterator();

        // Now, we will get the Iterable from Iterator
        Iterable < Double > iterable = convertIterableFromIterator(iterate);

        // Display converted Iterable elements
        iterable.forEach(System.out::println);
    }
}

Output

10.0
20.0
30.0
40.0





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.