Java Streams - Collectors averagingDouble (ToDoubleFunction mapper)








Collectors averagingDouble(ToDoubleFunction<? super T> mapper) returns a Collector that produces the arithmetic mean of a double-valued function applied to the input elements.

Syntax

averagingDouble has the following syntax.

public static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)

Example

The following example shows how to use averagingDouble.

import java.util.stream.Collectors;
import java.util.stream.Stream;
// w  w w .j  a va2  s .com
public class Main {
  public static void main(String[] args) {
    Stream<String> s = Stream.of("1","2","3");
    
    double o =  s.collect(Collectors.averagingDouble(n->Double.parseDouble(n)));

    System.out.println(o);
  }
}

The code above generates the following result.