public static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)
Returns a Collector that produces the arithmetic mean of a double-valued function applied to the input elements. If no elements are present, the result is 0.
Examples
The method Collectors#averagingDouble converts the stream elements to primitive double per user provided mapper function and returns the arithmetic mean of those doubles.
public class AveragingDoubleExample { public static void main (String[] args) { Stream<BigDecimal> s = Stream.iterate( BigDecimal.ONE, bigDecimal -> bigDecimal.add(BigDecimal.ONE)) .limit(10).peek(System.out::println);
Double d = s.collect(Collectors.averagingDouble(BigDecimal::doubleValue)); System.out.println("average: " + d); } }