Using a map operation to transform values
The following lines declare a new getGameNamesTransformedToUpperCase
method for our previously coded MemoryMobileGameRepository
class. The new method performs one of the simplest map operations. The call to the map
method transforms a Stream<MobileGame>
into a Stream<String>
.The lambda expression passed as an argument to the map
method generates a Function<MobileGame, String>
, that is, it receives a MobileGame
argument and returns a String
. The call to the collect
method generates a List<String>
from the Stream<String>
returned by the map
method.
The code file for the sample is included in the java_9_oop_chapter_12_01
folder, in the example12_10.java
file.
public List<String> getGameNamesTransformedToUpperCase() { return getAll().stream() .map(game -> game.name.toUpperCase()) .collect(Collectors.toList()); }
The getGameNamesTransformedToUpperCase
method returns a List<String>
. The map...