Transforming the elements of a stream
Some of the most useful intermediate operations you can use with streams are those that allow you to transform the elements of the stream. These operations receive elements of a class and return the elements of a different class. You can even change the type of stream and generate an IntStream
, LongStream
, or DoubleStream
from Stream
.
In this recipe, you will learn how to use the transforming intermediate operations provided by the Stream
class to convert its elements into a different class.
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...