DEV Community

Carlos A. Martinez
Carlos A. Martinez

Posted on

Streams Java

This concept was added to Java 8. Support functional-style operations on streams of elements, such as map-reduce transformations on collections.

What is Stream?
Stream is an immutable flow of elements, you could compare the way the streams process content to a loop.

Package java.util.stream;

Stream operations are divided into intermediate and terminal operations. A stream source can be a Collection, an array, a generator function.

Intermediate operations

The intermediate operations are lazy operations and return a new stream. Some examples at continuous:

- anyMatch()
- distinct()
- filter()
- findFirst()
- flatmap()
- map()
- skip()
- sorted()
- peek()
- limit()
Enter fullscreen mode Exit fullscreen mode

Terminal operations

The terminal operations terminate the stream source pipeline after performing the operations.

- count()
- max()
- min()
- reduce()
- summaryStatistics()
- forEach()
- forEachOrdered()
- sum()
- toArray()
- collect()
- anyMatch
- allMatch()
- noneMatch()
- findAny()
- findFirst()
Enter fullscreen mode Exit fullscreen mode

We can create stream via the stream() and parallelStream() methods and from an array via Arrays.stream(Object[]) method.

Interface Description
BaseStream> Base interface for streams, which are sequences of elements supporting sequential and parallel aggregate operations.
Collector A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed.
DoubleStream A sequence of primitive double-valued elements supporting sequential and parallel aggregate operations.
DoubleStream.Builder A mutable builder for a DoubleStream.
IntStream A sequence of primitive int-valued elements supporting sequential and parallel aggregate operations.
IntStream.Builder A mutable builder for an IntStream.
LongStream A sequence of primitive long-valued elements supporting sequential and parallel aggregate operations.
LongStream.Builder A mutable builder for a LongStream.
Stream A sequence of elements supporting sequential and parallel aggregate operations.
Stream.Builder A mutable builder for a Stream.

We can use these classes to collect the intermediate data and manipulate the stream.

Class Description
Collectors
Implementations of Collector that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
StreamSupport Low-level utility methods for creating and manipulating streams.

Example

IntStream
    .range(1, 10)
    .forEach(System.out::print);
System.out.println();
Enter fullscreen mode Exit fullscreen mode

[1,2,3,4,5,6,7,8,9]

Intermediate

Stream.of(18, 22, 5, 7, 14, 17, 33)
        .filter(x -> x > 18)
        .forEach(System.out::print);
Enter fullscreen mode Exit fullscreen mode

[22,33]

Terminal

    System.out.println("Min: " + IntStream.of(18, 22, 5, 7, 14, 17, 33).min());
    System.out.println("Max: " + IntStream.of(18, 22, 5, 7, 14, 17, 33).max());
    System.out.println("Count, Size, or Length: " + IntStream.of(18, 22, 5, 7, 14, 17, 33).count());
    System.out.println("Sum: " + IntStream.of(18, 22, 5, 7, 14, 17, 33).sum());
Enter fullscreen mode Exit fullscreen mode

[Min: OptionalInt[5]]
[Max: OptionalInt[33]]
[Count, Size, or Length: 7]
[Sum: 116]

Stream.of(18, 22, 5, 7, 14, 17, 33, 5, 22)
    .collect(Collectors.toSet())
    .forEach(System.out::println);
Enter fullscreen mode Exit fullscreen mode

[17,33,18,5,22,7,14,2]

Top comments (0)