O'Reilly logo

Test-Driven Java Development - Second Edition by Viktor Farcic, Alex Garcia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

@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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required