Terminal Stream Operations and reduce in Java
A terminal operation runs the pipeline and produces a result, and reduce is the general purpose way to fold a stream into one value.
Features from Java 8 onwards.
23 notes tagged
A terminal operation runs the pipeline and produces a result, and reduce is the general purpose way to fold a stream into one value.
filter, map, flatMap, sorted, distinct, limit, skip and peek. Each returns a new stream and none of them runs on its own.
A stream describes a computation over a sequence of elements. You state what you want, and the pipeline decides how to walk the data.
Optional makes absence part of the type, so a caller cannot forget that a value might not be there.
When a lambda does nothing but call an existing method, a method reference says the same thing with less noise.
An interface with exactly one abstract method can be implemented by a lambda, and the standard library supplies the ones you need most.
A lambda is a short way to write an implementation of a single method interface, so behaviour can be passed around like a value.
An annotation attaches metadata to code. It changes nothing by itself; a compiler, a tool or a framework reads it and acts.
A record declares an immutable data carrier, and the compiler generates the constructor, accessors, equals, hashCode and toString.
instanceof tests the runtime type, and since Java 16 it can bind a typed variable in the same step.
Decision making in Java, from the plain if statement to modern switch expressions and pattern matching.