Chapter 6. Data Processing with Streams
Almost any program has to deal with processing data, most likely in the form of Collections. An imperative approach uses loops to iterate over elements, working with each element in sequence. Functional languages, though, prefer a declarative approach and sometimes don’t even have a classical loop statement to begin with.
The Streams API, introduced in Java 8, provides a fully declarative and lazily evaluated approach to processing data that benefits from Java’s functional additions by utilizing higher-order functions for most of its operations.
This chapter will teach you the differences between imperative and declarative data processing. You will then have a visual introduction to Streams that highlights their underlying concepts and shows you how to get the most out of their flexibility to achieve a more functional approach to data processing.
Data Processing with Iteration
Processing data is an everyday task you’ve probably encountered a million times before and will continue to do in the future.
From a broad point of view, any type of data processing works like a pipeline, with a data structure like a Collection providing elements, one or more operations like filtering or transforming elements, and finally, delivering some form of a result. The result might be another data structure or even using it to run another task.
Let’s start with a simple data processing example.
External Iteration
Say that we need to find the three science ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access