Week 48 — What are `Stream`s (from `java.util.stream`) and how can they process data?
Question of the Week #48
What are
Stream
s (from java.util.stream
) and how can they process data?4 Replies
Stream
s allow processing data in a declarative way.
A Stream
represents a pipeline that starts at a source specifying where the data is coming from and ends with a terminal operation that collects (some of) the data or performs operations on it.
Streams utilize method chaining to chain together operations starting from the sink and ending at the terminal operation with an arbitrary amount of intermediate operations in between.
It is possible to get a Stream
from any Collection
by calling Collection#stream
. Similarly, it is possible to get a Stream
from an array using Arrays.stream
:
Common terminal operations include toList
which gets a List
with all elements of the Stream
or collect
which allows collecting the elements with a custom Collector
.
Similarly, it is possible to use finyAny
/findFirst
which get a single element from the Stream
wrapped in an Optional
or Optional.empty()
if there are no such elements:
Between the source and the terminal operations, it is possible to add intermediate operations. For example,
filter
can be used to only include elements matching a condition:
Another common intermediate operations is map
which transforms all object using a function:
Similar to that, the method flatMap
accepts a function that converts each object to a Stream
and allows processing all elements of all streams returned by the function.
📖 Sample answer from dan1st
The stream package provides a variety of things, the Stream API's work is to process complex data and to sum it up the data can be processed sequentially or parallely, there are more ways to process the data.
some key features are
stream()
this returns the source or collection or an array, and also i/o channels but we won't cover that
and then there's parallelStream()
to process parallely, the Stream API would also give us access to mappings, filtering and more of it's methods.⭐ Submission from xenofic