Stream mapToInt(ToIntFunction mapper) example

Description

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  . j  a v  a  2  s  .c o  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.





















Home »
  Java Streams »
    Reference »




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