Close

Java 8 Streams - StreamSupport.stream() Examples

Java 8 Streams Java Java API 


Class:

java.util.stream.StreamSupport

java.lang.Objectjava.lang.Objectjava.util.stream.StreamSupportjava.util.stream.StreamSupportLogicBig

Methods:

public static <T> Stream<T> stream(Spliterator<T> spliterator,
                                   boolean parallel)

Creates a new sequential or parallel Stream from a Spliterator.



public 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. This method also specifies the characteristics of the supplied Spliterator.

See also Iterator vs Spliterator.


Examples


package com.logicbig.example.streamsupport;

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

public class StreamExample {

public static void main(String... args) {
Set<String> set = new TreeSet<>(Arrays.asList("three", "one", "two"));
Stream<String> stream = StreamSupport.stream(set.spliterator(), false);
stream.forEach(System.out::println);
}
}

Output

one
three
two




package com.logicbig.example.streamsupport;

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

public class StreamExample2 {

public static void main(String... args) {
List<String> set = Arrays.asList("three", "one", "two");
Stream<String> stream = StreamSupport.stream(() -> set.spliterator(), Spliterator.ORDERED, false);
stream.forEach(System.out::println);
}
}

Output

three
one
two




See Also