Java Streams - Java Stream From Collections

Streams from Arrays

java.util.Arrays class contains stream() method to create sequential streams from arrays.

We can use it to create an IntStream, a LongStream, a DoubleStream, and a Stream<T>.

The following code creates an IntStream.

IntStream numbers = int[]{1, 2, 3});

The following code creates a Stream<String> from an int array and a String array.

Stream<String> names = String[] {"XML",   "Java"});

Streams from Collections

The Collection interface contains the stream() and parallelStream() methods that create sequential and parallel streams from a Collection, respectively.

The following code creates streams from a set of strings:

import java.util.HashSet;
import java.util.Set;
/*from  w w w . j a  va 2  s . co m*/
public class Main {
  public static void main(String[] args) {
    Set<String> names = new HashSet<>(); 

    Stream<String> sequentialStream  =;

    Stream<String> parallelStream = names.parallelStream();

The code above generates the following result.