Example usage for java.util.stream Collectors averagingInt

List of usage examples for java.util.stream Collectors averagingInt

Introduction

In this page you can find the example usage for java.util.stream Collectors averagingInt.

Prototype

public static <T> Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper) 

Source Link

Document

Returns a Collector that produces the arithmetic mean of an integer-valued function applied to the input elements.

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream<String> s = Stream.of("1", "2", "3");

    double o = s.collect(Collectors.averagingInt(n -> Integer.parseInt(n)));

    System.out.println(o);//  ww w  .  j a  va 2s. c  o m
}

From source file:Main.java

public static void main(String... args) {
    double o = Food.menu.stream().collect(Collectors.averagingInt(Food::getCalories));
    System.out.println(o);//w  w  w  .j a va2s. c  o  m

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Double averageAge = persons.stream().collect(Collectors.averagingInt(p -> p.age));

    System.out.println(averageAge); // 19.0
}