Stream map(Function mapper) example

Description

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;
//from w  w w.  ja v a2  s .  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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder