Java Streams - Collectors summingInt(ToIntFunction mapper) example








Collectors summingInt(ToIntFunction<? super T> mapper) returns a Collector that produces the sum of a integer-valued function applied to the input elements.

Syntax

summingInt has the following syntax.

public static <T> Collector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper)

Example

The following example shows how to use summingInt.

import java.util.stream.Collectors;
import java.util.stream.Stream;
//  w  w w  .  j a  va2 s.c  o m
public class Main {
  public static void main(String[] args) {
    Stream<String> s = Stream.of("1","2","3");
    
    int o =  s.collect(Collectors.summingInt(n->Integer.parseInt(n)));

    System.out.println(o);
  }
}

The code above generates the following result.