Java Streams - Stream mapToInt(ToIntFunction mapper) example








Stream mapToInt(ToIntFunction<? super T> mapper) returns an IntStream by applying the given function to this stream.

Syntax

mapToInt has the following syntax.

IntStream mapToInt(ToIntFunction<? super T> mapper)

Example

The following example shows how to use mapToInt.

import java.util.Arrays;
import java.util.List;
//from   www  .  ja v  a2s  .  co m
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("1","2","3","4","5");

    stringList.stream()
           .mapToInt(n-> Integer.parseInt(n) )
           .filter(n-> n%2 == 0)
           .forEach(System.out::println);
  }
}

The code above generates the following result.