Java Streams - Stream map(Function mapper) example








Stream map(Function<? super T,? extends R> mapper) returns a stream by applying the given function to this stream.

Syntax

map has the following syntax.

<R> Stream<R> map(Function<? super T,? extends R> mapper)

Example

The following example shows how to use map.

import java.util.Arrays;
import java.util.List;
// w ww. j  ava2s.co m
public class Main {
  public static void main(String[] args) {
    List<Integer> numberList = Arrays.asList(1,2,3,4,5);

    numberList.stream()
           .map(n-> n *2 )
           .forEach(System.out::println);
  }
}

The code above generates the following result.