Java - Streams from values

Introduction

The Stream interface contains the following two static of() methods to create a sequential Stream from a single value and multiple values:

<T> Stream<T> of(T t)
<T> Stream<T> of(T...values)

The following code creates two streams:

// Creates a stream with one string elements
Stream<String> stream = Stream.of("Hello");

// Creates a stream with four strings
Stream<String> stream = Stream.of("Java", "Jeff", "Chris", "Python");

The following code uses the Stream.of() method to create a stream from a list of values:

Demo

import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    // Compute the sum of the squares of all odd integers in the list
    int sum = Stream.of(1, 2, 3, 4, 5).filter(n -> n % 2 == 1).map(n -> n * n)
        .reduce(0, Integer::sum);

    System.out.println("Sum = " + sum);
  }/*w w w . ja  v  a  2s  .  co  m*/
}

Result

The second version of the of() method takes a varargs argument and you can use it to create a stream from an array of objects.

The following code creates a stream from a String array.

String[] names  = {"Java", "Jeff", "Chris", "Python"};

// Creates a stream of four strings in the names array
Stream<String> stream = Stream.of(names);

Stream.of() method creates a stream whose elements are of reference type.

To create a stream of primitive values from an array of primitive type, use the Arrays.stream() method.

The following code creates a stream of strings from a String array returned from the split() method of the String class:

String str  = "Java,Jeff,Chris,Python";

// The stream will contain four elements: "Java", "Jeff", "Chris", and "Python"
Stream<String> stream = Stream.of(str.split(","));

Builder

Stream interface supports creating a stream using the builder pattern using the Stream.Builder<T> interface.

The builder() static method of the Stream interface returns a stream builder.

// Gets a stream builder
Stream.Builder<String> builder = Stream.builder();

The following code uses the builder pattern to create a stream of four strings:

import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    Stream<String> stream = Stream.<String>builder()
        .add("Java")
        .add("Jeff")
        .add("Chris")
        .add("Python")
        .build();

  }
}

The IntStream interfaces contain two static methods:

IntStream range(int start, int end)
IntStream rangeClosed(int start, int end).

They produce an IntStream that contains ordered integers between the specified start and end.

The specified end is exclusive in the range() method whereas it is inclusive in the rangeClosed() method.

The following code uses both methods to create an IntStream having integers 1, 2, 3, 4, and 5 as their elements:

// Create an IntStream containing 1, 2, 3, 4, and 5
IntStream oneToFive = IntStream.range(1, 6);

// Create an IntStream containing 1, 2, 3, 4, and 5
IntStream oneToFive = IntStream.rangeClosed(1, 5);

LongStream class also contains range() 0and rangeClosed() methods that takes arguments of type long and return a LongStream.