March 2018
Intermediate to advanced
324 pages
8h 30m
English
The map function transforms all the elements in the stream into another. The resultant object can share the type with the input, but it's also possible to return an object of a different type:
@Testpublic void mapToUppercaseTransformsAllElementsToUppercase() { List<String> names = Arrays.asList("Alex", "Paul", "Viktor"); List<String> namesUppercase = Collections.emptyList(); assertThat(namesUppercase) .hasSize(3) .containsExactly("ALEX", "PAUL", "VIKTOR");}
The list namesUppercase should be computed as follows:
List<String> namesUppercase = names.stream() .map(String::toUpperCase) .collect(Collectors.toList());
Note how the toUpperCase method is called. It belongs to the Java class String and can be used in that scenario only by referencing ...
Read now
Unlock full access