map

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:

@Test
public 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 the function and the class that function belongs to. In Java, this is called method reference.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.236