Streams

A java.util.Stream represents a sequence of elements on which one or more operations can be performed. Stream operations are either intermediate or terminal. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a java.util.Collection like lists or sets (maps are not supported). Stream operations can either be executed sequentially or parallely.

Streams are extremely powerful, so I wrote a separate Java 8 Streams Tutorial. You should also check out Sequency as a similiar library for the web.

Let’s first look how sequential streams work. First we create a sample source in form of a list of strings:

  1. List<String> stringCollection = new ArrayList<>();
  2. stringCollection.add("ddd2");
  3. stringCollection.add("aaa2");
  4. stringCollection.add("bbb1");
  5. stringCollection.add("aaa1");
  6. stringCollection.add("bbb3");
  7. stringCollection.add("ccc");
  8. stringCollection.add("bbb2");
  9. stringCollection.add("ddd1");

Collections in Java 8 are extended so you can simply create streams either by calling Collection.stream() or Collection.parallelStream(). The following sections explain the most common stream operations.

Filter

Filter accepts a predicate to filter all elements of the stream. This operation is intermediate which enables us to call another stream operation (forEach) on the result. ForEach accepts a consumer to be executed for each element in the filtered stream. ForEach is a terminal operation. It’s void, so we cannot call another stream operation.

  1. stringCollection
  2. .stream()
  3. .filter((s) -> s.startsWith("a"))
  4. .forEach(System.out::println);
  5. // "aaa2", "aaa1"

Sorted

Sorted is an intermediate operation which returns a sorted view of the stream. The elements are sorted in natural order unless you pass a custom Comparator.

  1. stringCollection
  2. .stream()
  3. .sorted()
  4. .filter((s) -> s.startsWith("a"))
  5. .forEach(System.out::println);
  6. // "aaa1", "aaa2"

Keep in mind that sorted does only create a sorted view of the stream without manipulating the ordering of the backed collection. The ordering of stringCollection is untouched:

  1. System.out.println(stringCollection);
  2. // ddd2, aaa2, bbb1, aaa1, bbb3, ccc, bbb2, ddd1

Map

The intermediate operation map converts each element into another object via the given function. The following example converts each string into an upper-cased string. But you can also use map to transform each object into another type. The generic type of the resulting stream depends on the generic type of the function you pass to map.

  1. stringCollection
  2. .stream()
  3. .map(String::toUpperCase)
  4. .sorted((a, b) -> b.compareTo(a))
  5. .forEach(System.out::println);
  6. // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"

Match

Various matching operations can be used to check whether a certain predicate matches the stream. All of those operations are terminal and return a boolean result.

  1. boolean anyStartsWithA =
  2. stringCollection
  3. .stream()
  4. .anyMatch((s) -> s.startsWith("a"));
  5. System.out.println(anyStartsWithA); // true
  6. boolean allStartsWithA =
  7. stringCollection
  8. .stream()
  9. .allMatch((s) -> s.startsWith("a"));
  10. System.out.println(allStartsWithA); // false
  11. boolean noneStartsWithZ =
  12. stringCollection
  13. .stream()
  14. .noneMatch((s) -> s.startsWith("z"));
  15. System.out.println(noneStartsWithZ); // true

Count

Count is a terminal operation returning the number of elements in the stream as a long.

  1. long startsWithB =
  2. stringCollection
  3. .stream()
  4. .filter((s) -> s.startsWith("b"))
  5. .count();
  6. System.out.println(startsWithB); // 3

Reduce

This terminal operation performs a reduction on the elements of the stream with the given function. The result is an Optional holding the reduced value.

  1. Optional<String> reduced =
  2. stringCollection
  3. .stream()
  4. .sorted()
  5. .reduce((s1, s2) -> s1 + "#" + s2);
  6. reduced.ifPresent(System.out::println);
  7. // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"