Java Streams - Collectors averagingLong (ToLongFunction mapper)








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

Syntax

averagingLong has the following syntax.

public static <T> Collector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper)

Example

The following example shows how to use averagingLong.

import java.util.stream.Collectors;
import java.util.stream.Stream;
// ww  w.j  ava  2  s  .c om
public class Main {
  public static void main(String[] args) {
    Stream<String> s = Stream.of("1","2","3");
    
    double o =  s.collect(Collectors.averagingLong(n->Long.parseLong(n)));

    System.out.println(o);
  }
}

The code above generates the following result.