The Java Stream API: Parallel Streams
The sample code for this post can be found on GitHub.
Parallel Streams are simple to generate in Java. Instead of calling .stream(), you simply call parallelStream(). Here, we’ll take our familiar list of names:
The sample code for this post can be found on GitHub.
Parallel Streams are simple to generate in Java. Instead of calling .stream(), you simply call parallelStream(). Here, we’ll take our familiar list of names:
The sample code for this post can be found on GitHub.
While Streams in Java would typically be used on POJO or POJO-like data structures, Java also lets us deal directly with primitive type streams. Thanks to Java’s type erasure, something like Stream would not work, as the type argument must be an Object.
The sample code for this post can be found on GitHub.
Reduction operations are a way to consolidate collections into one simple result.
You can view the sample code associated with this post on GitHub.
One interesting feature of collecting streams using the Collectors.groupingBy(..)
method is the ability to manipulate downstream elements. Basically,
this means that, after you group the keys of the map, you can further make changes to the collection that the map is pointing to. By default, it collects to a list.
You can view the sample code associated with this post on GitHub.
Taking a stream of POJOs and transforming it into a map is one of the more common uses of Streams.
There are a few built in ways to get what you want. The simplest way to map one field to another in a POJO is by
using the .toMap(..)
method.
You can view the sample code associated with this post on GitHub.
Calling collect(..)
on a stream terminates a stream into a collection. We’ve already seen that calling collect(Collectors.toList()) moves your stream into
a List, but you can also collect into a set. If we take our familiar collection of names in a String collection:
You can view the sample code associated with this post on GitHub.
While Optionals are often used in conjunction with the Java Stream API, you can also create your own. Now, why would you want to do that? Simply put, null pointer exceptions are not a fun time, and embracing optional types will greatly simplify code development, and prevent premature graying hair.
You can find the sample code associated with this post on GitHub.
If Java programmers had a generic Facebook page, they would collectively have an “it’s complicated” relationship with the null value.
The sample code associated with this post can be found on GitHub.
Sometimes, we will have two different streams of data that we want to aggregate into one stream to analyze. In that case, we can use the Stream.concat(..)
method. Here, if we take our list of names from before:
You can find the sample code for this post on GitHub.
From the knowledge gained via creating custom Java Stream objects, we can start to have a little bit of fun with this. The fibonacci number sequence starts with [0, 1], and adds each of the previous two elements to create the next element in the sequence. This looks like [0, 1, 1, 2, 3, 5, 8, 13, 21…], and goes on “forever.” We can thus create a template that computes all Fibonacci numbers by implementing a Supplier. like so:
You can find the sample code from this post on GitHub.
There are a few built in ways to create your own custom streams. While many collections offer a direct .stream()
method,
you can also use Stream.of(..)
to just make one in place:
The sample code provided with this series on the Java Stream API can be retrieved on GitHub.