Collectors summingLong(ToLongFunction mapper) example

Description

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

Syntax

summingLong has the following syntax.


public static <T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)

Example

The following example shows how to use summingLong.


import java.util.stream.Collectors;
import java.util.stream.Stream;
/*from  w ww  .ja  v a2s .c  o m*/
public class Main {
  public static void main(String[] args) {
    Stream<String> s = Stream.of("1","2","3");
    
    long o =  s.collect(Collectors.summingLong(n->Long.parseLong(n)));

    System.out.println(o);
  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Reference »




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