16.1 Introduction to Streams

A stream allows aggregate operations to be performed on a sequence of elements. An aggregate operation performs a task on the stream as a whole rather than on an individual element of the stream. In the context of streams, these aggregation operations are called stream operations. Such operations utilize behavior parameterization implemented by functional interfaces for actions performed on the stream elements.

Examples of stream operations accepting implementation of functional interfaces include:

  • Generating elements of the stream using a Supplier
  • Converting the elements in the stream according to a mapping defined by a Function
  • Filtering the elements in the stream according to some criteria defined by a Predicate
  • Sorting the elements in the stream using a Comparator
  • Performing actions for each of the elements in the stream with the help of a Consumer

Streams can be produced from a variety of sources. Collections and arrays are typical examples of sources for streams. The Collection<E> interface and the Arrays utility class both provide a stream() method that builds a stream from the elements of a collection or an array.

In the loop-based solution below, elements from the values list are processed using a for(:) loop to test whether a year is after the year 2000. The strings in the list are parsed to a Year object before being tested in an if statement.

Click here to view code image

// Loop-based solution:
List<String> values = List.of(“2001”, “1999”, “2021”);
for (String s : values) {
  Year y = Year.parse(s);
  if (y.isAfter(Year.of(2000))) {
    System.out.print(s + ” “);                      // 2001 2021
  }
}
// Stream-based solution:
List<String> values2 = List.of(“2001”, “1999”, “2021”);
values2.stream()                                    // (1)
       .map(s -> Year.parse(s))                     // (2)
       .filter(y -> y.isAfter(Year.of(2000)))       // (3)
       .forEach(y -> System.out.print(y + ” “));    // (4) 2001 2021

A stream-based solution for the same problem is also presented above. The stream() operation at (1) generates a stream based on the elements from the collection. The map() operation at (2) parses the string elements to a Year object, as defined by the lambda expression that implements the Function interface. The filter() operation at (3) performs a filtering of the elements in the stream that are after the year 2000, as defined by a lambda expression that implements the Predicate interface. The forEach() operation at (4) performs an action on each stream element, as defined by a lambda expression that implements the Consumer interface.

The loop-based solution specifies how the operations should be performed. The stream-based solution states what operations should be performed, qualified by the implementation of an appropriate functional interface. Stream-based solutions to many problems can be elegant and concise compared to their iteration-based counterparts.

In this chapter we will cover many stream operations in detail, as well as discover other use cases and benefits of using streams.

Leave a Reply

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