Java 8 – How to convert Iterator to Stream

In Java 8, we can use StreamSupport.stream to convert an Iterator into a Stream.


// Iterator -> Spliterators -> Stream
  Stream<String> stream = StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(
                        iterator,
                        Spliterator.ORDERED)
                , false);

Review the StreamSupport.stream method signature, it accepts a Spliterator.

StreamSupport.java

  public static <T> Stream<T> stream(Spliterator<T> spliterator, boolean parallel) {
      Objects.requireNonNull(spliterator);
      return new ReferencePipeline.Head<>(spliterator,
                                          StreamOpFlag.fromCharacteristics(spliterator),
                                          parallel);
  }

1. Iterator -> Stream

The Iterator interface has no spliterator() method, so we need to use Spliterators.spliteratorUnknownSize to convert the Iterator into a Spliterator, followed by StreamSupport.stream to convert the Spliterator into a Stream.


Iterator -> Spliterators -> Stream

This example converts an Iterator into a Stream, modify the values, and return a List.

JavaStreamExample1.java

package com.mkyong;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class JavaStreamExample1 {

    public static void main(String[] args) {

        List<String> list = new ArrayList<>();
        list.add("mkyong");
        list.add("java");
        list.add("kotlin");
        list.add("spring boot");
        list.add("android");

        Iterator<String> iterator = list.iterator();

        // Iterator -> Spliterators -> Stream -> List
        List<Integer> result = StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
                .map(x -> x.length())
                .collect(Collectors.toList());

        result.forEach(x -> System.out.println(x));
        System.out.println(result.getClass());

    }

}

Output


6
4
6
11
7
class java.util.ArrayList

2. Iterable -> Stream

Since the Iterable interface has a spliterator() method and it is easier to convert it into a Stream.

JavaStreamExample2.java

package com.mkyong;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class JavaStreamExample2 {

    public static void main(String[] args) {

        Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4, 5);

        // Iterable -> Spliterators -> Stream -> List
        List<Integer> result = StreamSupport.stream(
                iterable.spliterator(), false)
                .map(x -> x * 10)
                .collect(Collectors.toList());

        result.forEach(x -> System.out.println(x));
        System.out.println(result.getClass());

    }

}

Output


10
20
30
40
50

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar