Convert Iterable or Iterator to Stream in Java

Learn to convert Iterable or Iterator to Stream. It may be desired at times when we want to utilize excellent support of lambda expressions and collectors in Java 8 Stream API.

1. Iterable to Stream

Iterables are useful but provide limited support for lambda expressions added in Java 8. To utilize full language features, it is desired to convert the iterable to stream.

To convert, we will use iterable.spliterator() method to get the Spliterator reference, which is then used to get the Stream using StreamSupport.stream(spliterator, isParallel) method.

import java.util.Arrays;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class IterableToStream 
{
	public static void main(String[] args) 
	{
		//Iterable
		Iterable<String> iterable = Arrays.asList("a", "b", "c");
		
		//Iterable -> Stream
		//false means sequential stream
		Stream<String> stream = StreamSupport.stream(iterable.spliterator(), false);
		
		//Apply stream operations
		stream.forEach(System.out::println);
	}
}

Program output:

a
b
c

The above code is only linking the Stream to the Iterable but the actual iteration won’t happen until a terminal operation.

The second argument in StreamSupport.stream() determines if the resulting Stream should be parallel or sequential. Set it true for a parallel stream and false for sequential stream.

2. Iterator to Stream – Java 8

Iterator to stream follows the same path as Iterable to stream.

The only difference is that the Iterator interface has no spliterator() method so we need to use Spliterators.spliteratorUnknownSize() method to get the spliterator. Rest everything is same.

import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class IteratorToStream 
{
	public static void main(String[] args) 
	{
		// Iterator
		Iterator<String> iterator = Arrays.asList("a", "b", "c")
											.listIterator();
		//Extra step to get Spliterator
		Spliterator<String> splitItr = Spliterators
				.spliteratorUnknownSize(iterator, Spliterator.ORDERED);

		// Iterator -> Stream
		Stream<String> stream = StreamSupport.stream(splitItr, false);

		// Apply stream operations
		stream.forEach(System.out::println);
	}
}

Program output:

a
b
c

3. Iterator to Stream – Java 9

Java 9 has made the syntax a little easier and now we don’t need to use Spliterator explicitly. Rather it uses a Predicate to decide until when the elements shall be taken.

import java.util.Arrays;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class IteratorToStream 
{
	public static void main(String[] args) 
	{
		// Iterator
		Iterator<String> iterator = Arrays.asList("a", "b", "c")
											.listIterator();
		Stream.generate(() -> null)
		    .takeWhile(x -> iterator.hasNext())
		    .map(n -> iterator.next())
		    .forEach(System.out::println);
	}
}

Program output:

a
b
c

Drop me your questions in comments.

References:

mail.openjdk.java.net
SO Thread

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.