Verifying conditions in the elements of a stream
One interesting option provided by the Stream
class is the possibility to check if the elements of the stream verify a condition or not. This functionality is provided by the terminal operations that return a Boolean
value.
In this recipe, you will learn which methods provide the Stream
class to check conditions in the elements of a stream and how to use them.
Getting ready
The example of this recipe has been implemented using the Eclipse IDE. If you use Eclipse or other IDE such as NetBeans, open it and create a new Java project.
How to do it...
Follow these steps to implement the example:
- First, we'll implement some auxiliary classes we will use in the example. First, implement the
Person
class, which stores the basic attributes of a person, and thePersonGenerator
class, which generates aList
of randomPerson
objects. Please, check the recipe Apply an action to all the elements of a stream to see the source code of both classes. - Then, create...