Using the Stream class methods
In this section, we will explore how the Stream
class methods are used to solve various types of problems. Streams are useful for transforming stream elements, filtering elements, and reducing elements. They can mimic SQL-type processing and implement the map-reduce paradigm, which we will illustrate in the Implementing the map-reduce paradigm section.
Filter methods
The process of filtering involves iterating over a sequence and eliminating those elements that are no longer needed. We will examine how this is accomplished using an imperative loop and then how it is performed using streams.
Assume that we want to filter out the plural names in an animal list. We start with an array of strings containing animal names. Each element of the array will be matched against a regular expression to determine if ends with an "s"
. If it is not plural, it will be added to a list as shown here:
String[] animals = {"cats", "dog", "ox"...