Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 770 Bytes

File metadata and controls

28 lines (19 loc) · 770 Bytes

map

Once you have a stream of elements you can transform the elements of the stream as they flow by1 with .map.

.map applies a Function to the elements of the stream one by one and returns you a new Stream containing the new elements.

var numbers = List.of("1", "2", "3");

Stream<Integer> numberStream = numbers.stream()
    .map(Integer::parseInt); // 1, 2, 3

You can also call .map multiple times to apply multiple transformations.

var numbers = List.of("1", "2", "3");

Stream<Integer> numberStream = numbers.stream()
    .map(Integer::parseInt)  // 1, 2, 3
    .map(x -> x * 2); // 2, 4, 6

Footnotes

  1. In the real life stream metaphor, this is akin to rocks getting polished by sand as they flow.