Working with different collectors
We can follow a functional approach and solve different kinds of algorithms with stream processing pipelines and the help of the diverse collectors provided by Java 9, that is, the diverse static methods provided by the java.util.stream.Collectors
class. In the next examples, we will use different arguments for the collect
method.
The following lines join all the names for the MobileGame
instances to generate a single String
with the names separated with a separator ("; "
). The code file for the sample is included in the java_9_oop_chapter_12_01
folder, in the example12_21.java
file.
repository.getAll().stream() .map(game -> game.name.toUpperCase()) .collect(Collectors.joining("; "));
The code passes Collectors.joining(";" )
as an argument to the collect
method. The joining
static method returns a Collector
that concatenates the input elements into a String
separated by the delimiter received as an argument. The following shows...