Chapter 7. Working with Streams
Streams utilize many of the functional features introduced in Java 8 to provide a declarative way to process data. The Stream API covers many use cases, but you need to know how the different operations and available helper classes work to make the most of them.
Chapter 6 concentrated on showing you the foundation of Streams. This chapter will build on that and teach you different ways to create and work with Streams for various use cases.
Primitive Streams
In Java, generics only work with object-based types (yet1).
That’s why Stream<T>
can’t be used for sequences of primitive values like int
.
There are only two options for using primitive types with Streams:
-
Autoboxing
-
Specialized Stream variants
Java’s autoboxing support—the automatic conversion between primitive types and their object-based counterparts like int
and Integer
—may seem like a simple workaround because it automagically works, as shown as follows:
Stream
<
Long
>
longStream
=
Stream
.
of
(
5L
,
23L
,
42L
);
Autoboxing introduces multiple problems, though. For one, there’s the overhead associated with the conversion from primitive values to objects compared to using primitive types directly. Usually, the overhead is negligible. Still, in a data processing pipeline, the overhead of such frequent creation of wrapper types accumulates and can degrade overall performance.
Another nonissue with primitive wrappers is the possibility of null
elements. The direct conversion from primitive to ...
Get A Functional Approach to Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.