Friday, August 16, 2019

Java 8 StreamSupport Examples

1. Overview


In this tutorial, We'll be learning Java 8 StreamSupport class methods with example programs. Let us see its syntax and how to convert iterable to Stream using StreamSupport.stream() method. This class is part of java.util.stream package.

Core methods of StreamSupport class are stream(), intStream(), doubleStream(), longStream().



2. Methods


This has only static methods just like utility class. These are for creating and manipulating streams.

static DoubleStream doubleStream(Spliterator.OfDouble spliterator, boolean parallel): Creates a new sequential or parallel DoubleStream from a Spliterator.OfDouble.
static DoubleStream doubleStream(Supplier<? extends Spliterator.OfDouble> supplier, int characteristics, boolean parallel): Creates a new sequential or parallel DoubleStream from a Supplier of Spliterator.OfDouble.
static IntStream intStream(Spliterator.OfInt spliterator, boolean parallel): Creates a new sequential or parallel IntStream from a Spliterator.OfInt.
static IntStream intStream(Supplier<? extends Spliterator.OfInt> supplier, int characteristics, boolean parallel): Creates a new sequential or parallel IntStream from a Supplier of Spliterator.OfInt.
static LongStream longStream(Spliterator.OfLong spliterator, boolean parallel): Creates a new sequential or parallel LongStream from a Spliterator.OfLong.
static LongStream longStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, boolean parallel): Creates a new sequential or parallel LongStream from a Supplier of Spliterator.OfLong.
static <T> Stream<T> stream(Spliterator<T> spliterator, boolean parallel): Creates a new sequential or parallel Stream from a Spliterator.
static <T> Stream<T> stream(Supplier<? extends Spliterator<T>> supplier, int characteristics, boolean parallel): Creates a new sequential or parallel Stream from a Supplier of Spliterator.

3. Examples

3.1 Java program to covert list split iterator to Stream


This program creates a sequential stream which processes the elements as they appear in the underlying List.

package com.java.w3schools.blog.java8.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * Java 8 StreamSupport Examples F
 * 
 * @author venkateshn
 *
 */
public class StreamSupportExamples {

 public static void main(String[] args) {

  List<String> valuesList = new ArrayList<>();
  valuesList.add("java 8");
  valuesList.add("example");
  valuesList.add("blog");
  valuesList.add("2019");

  Stream<String> stream = StreamSupport.stream(valuesList.spliterator(), false);
  stream.forEach(System.out::println);
 }

}

Output:


java 8
example
blog
2019

Here, the output printed in the same input order.

3.2 Creating parallel stream using StreamSupport.stream() method


An example is the same as an above program but it takes argument parallel as true in stream() method.
True indicates that it creates a new parallel stream instead of a sequential stream.


List<String> valuesList1 = new ArrayList<>();
valuesList1.add("java 8");
valuesList1.add("example");
valuesList1.add("blog");
valuesList1.add("2019");

Stream<String> stream1 = StreamSupport.stream(valuesList1.spliterator(), true);
stream1.forEach(System.out::println);

Output:

blog
2019
example
java 8

Compare this output with the previous section output. Both are not in the same order. The current output is generated by the parallel stream.

3.3 steam() method using characteristics option


Syntax:

public static <T> Stream<T> stream(Supplier<? extends Spliterator<T>> supplier, int characteristics, boolean parallel)

The first argument takes the Supplier implementation that takes Spliterator.
characteristics - Spliterator characteristics of the supplied Spliterator. The characteristics must be equal to supplier.get().characteristics(), otherwise undefined behavior may occur when terminal operation commences.
parallel - if true then the returned stream is a parallel stream; if false the returned stream is a sequential stream.

Program:

Set<String> valuesList2 = new TreeSet<>();
valuesList2.add("java 8");
valuesList2.add("example");
valuesList2.add("blog");
valuesList2.add("2019");

Supplier<Spliterator> supplier = () -> valuesList2.spliterator();
System.out.println("supplier characteristics value : " + supplier.get().characteristics());
Stream<String> stream2 = StreamSupport.stream(() -> valuesList2.spliterator(), supplier.get().characteristics(),
  false);
stream2.forEach(System.out::println);

Output:

supplier characteristics value : 85
2019
blog
example
java 8 

3.4 intStream(), doubleStream(), longStream() method examples


System.out.println("\nStreamSupport intStream example:");
IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Supplier intSupplier = () -> intStream.spliterator();
IntStream finalIntStream = StreamSupport.intStream(intSupplier, Spliterator.ORDERED, false);
finalIntStream.forEach(System.out::println);

System.out.println("\nStreamSupport intStream example:");
DoubleStream doubleStream = DoubleStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Supplier doubleSupplier1 = () -> doubleStream.spliterator();
DoubleStream finalDoubleStream = StreamSupport.doubleStream(doubleSupplier1, Spliterator.ORDERED, false);
finalDoubleStream.forEach(System.out::println);

System.out.println("\nStreamSupport LongStream example:");
LongStream longStream = LongStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Supplier longSupplier = () -> longStream.spliterator();
LongStream finalLongStream = StreamSupport.longStream(longSupplier, Spliterator.ORDERED, false);
finalLongStream.forEach(System.out::println);

Output:

StreamSupport intStream example:
1
2
3
4
5
6
7
8
9
10

StreamSupport intStream example:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0

StreamSupport LongStream example:
1
2
3
4
5
6
7
8
9
10


4. Conclusion


In this article, We've seen example programs on StreamSupport.stream() methods to create sequential and parallel streams.

And also example programs on intStream(), doubleStream(), longStream() methods.

Internally these methods use DoublePipeline, IntPipeline, LongPipeline respectively and stream() method uses ReferencePipeline class internally to create a Stream.

GitHub
API Ref

No comments:

Post a Comment