Overview of API for Data Processing Using Streams

In this subsection we present a brief overview of new interfaces and classes that are introduced in this chapter. We focus mainly on the Stream API in the java.util.stream package, but we also discuss utility classes from the java.util package.

The Stream Interfaces

Figure 16.2 shows the inheritance hierarchy of the core stream interfaces that are an important part of the Stream API defined in the java.util.stream package. The generic interface Stream<T> represents a stream of object references—that is, object streams. The interfaces IntStream, LongStream, and DoubleStream are specializations to numeric streams of type int, long, and double, respectively. These interfaces provide the static factory methods for creating streams from various sources (p. 890), and define the intermediate operations (p. 905) and the terminal operations (p. 946) on streams.

The interface BaseStream defines the basic functionality offered by all streams. It is recursively parameterized with a stream element type T and a subtype S of the BaseStream interface. For example, the Stream<T> interface is a subtype of the parameterized BaseStream<T, Stream<T>> interface, and the IntStream interface is a subtype of the parameterized BaseStream<Integer, IntStream> interface.

All streams implement the AutoCloseable interface, meaning they should be closed after use in order to facilitate resource management during execution. However, this is not necessary for the majority of streams. Only resource-backed streams need to be closed—for example, a stream whose data source is a file. Such resources are best managed automatically with the try-with-resources statement (§7.7, p. 407).

Figure 16.2 The Core Stream Interfaces

The Collectors Class

A collector encapsulates the machinery required to perform a reduction operation (p. 978). The java.util.stream.Collector interface defines the functionality that a collector must implement. The java.util.stream.Collectors class provides a rich set of predefined collectors for various kinds of reductions.

The Optional Classes

Instances of the java.util.Optional<T> class are containers that may or may not contain an object of type T (p. 940). An Optional<T> instance can be used to represent the absence of a value of type T more meaningfully than the null value. The numeric analogues are OptionalInt, OptionalLong, and OptionalDouble that can encapsulate an int, a long, or a double value, respectively.

The Numeric Summary Statistics Classes

Instances of the IntSummaryStatistics, LongSummaryStatistics, and DoubleSummary-Statistics classes in the java.util package are used by a group of reduction operations to collect summarizing statistics like the count, sum, average, min, and max of the values in a numeric stream of type int, long, and double, respectively (p. 974, p. 1001).

Leave a Reply

Your email address will not be published. Required fields are marked *