Chapter 5. Working with streams

This chapter covers

  • Filtering, slicing, and matching
  • Finding, matching, and reducing
  • Using numeric streams such as ranges of numbers
  • Creating streams from multiple sources
  • Infinite streams

In the previous chapter, you saw that streams let you move from external iteration to internal iteration. Instead of writing code as follows where you explicitly manage the iteration over a collection of data (external iteration),

List<Dish> vegetarianDishes = new ArrayList<>();
for(Dish d: menu){
    if(d.isVegetarian()){
        vegetarianDishes.add(d);
    }
}

you can use the Streams API (internal iteration), which supports the filter and collect operations, to manage the iteration over the collection of data for you. All you need ...

Get Java 8 in Action: Lambdas, streams, and functional-style programming 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.