Chapter 3. Adopting Streams
In this chapter, you’ll learn how to adopt the Streams API. First, you’ll gain an understanding behind the motivation for the Streams API, and then you’ll learn exactly what a stream is and what it’s used for. Next, you’ll learn about various operations and data processing patterns using the Streams API, and about Collectors, which let you write more sophisticated queries. You’ll then look at a practical refactoring example. Finally, you’ll learn about parallel streams.
The Need for Streams
The Collections API is one of the most important parts of the Java API. Nearly every Java application makes and processes collections. But despite its importance, the processing of collections in Java is still unsatisfactory in many aspects.
For one reason, many alternative programming languages or libraries let you express typical data processing patterns in a declarative way. Think of SQL, where you can select from a table, filter values given a condition, and also group elements in some form. There’s no need to detail how to implement the query—the database figures it out for you. The benefit is that your code is easier to understand. Unfortunately, in Java you don’t get this. You have to implement the low-level details of a data processing query using control flow constructs.
Second, how can you process really large collections efficiently? Ideally, to speed up the processing, you want to leverage multicore architectures. However, writing parallel code is hard ...
Get Introducing Java 8 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.