Chapter 6. Collecting data with streams

 

This chapter covers

  • Creating and using a collector with the Collectors class
  • Reducing streams of data to a single value
  • Summarization as a special case of reduction
  • Grouping and partitioning data
  • Developing your own custom collectors

You learned in the previous chapter that streams help you process collections with database-like operations. You can view Java 8 streams as fancy lazy iterators of sets of data. They support two types of operations: intermediate operations such as filter or map and terminal operations such as count, findFirst, forEach, and reduce. Intermediate operations can be chained to convert a stream into another stream. These operations don’t consume from a stream; their purpose is to set up a pipeline of streams. By contrast, terminal operations do consume from a stream—to produce a final result (for example, returning the largest element in a stream). They can often shorten computations by optimizing the pipeline of a stream.

We already used the collect terminal operation on streams in chapters 4 and 5, but we employed it there mainly to combine all the elements of a stream into a List. In this chapter, you’ll discover that collect is a reduction operation, like reduce, that takes as an argument various recipes for accumulating the elements of a stream into a summary result. These recipes are defined by a new Collector interface, so it’s important to distinguish Collection, Collector, and collect!

6.1. Collectors in a nutshell

6.2. Reducing and summarizing

6.3. Grouping

6.4. Partitioning

6.5. The Collector interface

6.6. Developing your own collector for better performance

Summary