Java - Streams API Architecture

Introduction

Stream<T> interface takes a type parameter T.

To work with a stream of primitive type such as int, long, etc. use XXXStream such as LongStream, and DoubleStream to work with primitives.

The stream() method from Collection interface returns a sequential stream where the Collection acts as the data source.

The following code creates a List<Integer> and obtains a Stream<Integer> from the list:

// Get a list of integers from 1 to 5
List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);

// Get the stream from the list
Stream<Integer> numbersStream = numbersList.stream();

filter() method of the Stream<T> interface takes a Predicate<T> as argument and returns a Stream<T> with elements filtered by Predicate.

The following statement obtains a stream of only odd integers:

// Get a stream of odd integers
Stream<Integer> oddNumbersStream= numbersStream.filter(n -> n % 2 == 1);

It is using the lambda expression as the argument for the filter() method.

The lambda expression returns true if the element in the stream is not divisible by 2.

map() method of the Stream<T> interface takes a Function as argument.

Each element in the stream is passed to the Function and a new stream is generated containing the returned values from the Function.

The following statement takes all odd integers and maps them to their squares:

// Get a stream of the squares of odd integers
Stream<Integer> squaredNumbersStream = oddNumbersStream.map(n -> n * n);

reduce(T identity, BinaryOperator<T> accumulator) method of the Stream interface performs a reduction operation to a single value.

It takes an initial value and an accumulator that is a BinaryOperator<T> as arguments.

For initial run, the accumulator receives the initial value and the first element of the stream as arguments, and returns a value.

Then the accumulator receives the value returned from its previous call and the second element from the stream.

This process continues until all elements of the stream have been passed to the accumulator.

The returned value from the last call of the accumulator is returned from the reduce() method.

The following code performs the summation of all integers in the stream:

// Sum all integers in the stream
int sum = squaredNumbersStream.reduce(0, (n1, n2) -> n1 + n2);

Integer class static sum() method performs sum of two integers.

You can rewrite the code using a method reference:

// Sum all integers in the stream
int sum = squaredNumbersStream.reduce(0, Integer::sum);

The following code combines these statements into one statement as follows:

Demo

import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
 // Sum all integers in the numbers list
    int sum = numbers.stream()
                     .filter(n -> n %2 ==1)
                     .map(n -> n * n)
                     .reduce(0, Integer::sum);

    System.out.println(sum);//w  ww  .  j av  a 2s  .  c o  m
  }
}

Result

Related Topics