June 29, 2022

Java Stream Collectors averagingInt(), averagingLong(), averagingDouble()

If you want average of Stream elements there are handy utility methods available in Collectors class to quickly do that, the methods are-

  • Collectors.averagingInt()- To get average of stream of integers.
  • Collectors.averagingLong()- To get average of stream of longs.
  • Collectors.averagingDouble()- To get average of stream of doubles.

Collectors.averaging methods in Java

Syntax of these methods is as given below-

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

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

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

All of these methods return a Collector that produces the arithmetic mean of the values.

Argument passed is of type ToIntFunction, ToLongFunction and ToDoubleFunction respectively. These are functional interfaces with methods applyAsInt(T value), applyAsLong(T value), applyAsDouble(T value) that produces a int-valued, long-valued and double-valued result.

Collectors.averagingInt() example

In this example we’ll get the average of List elements where List is storing integers.

public class CollectorAveragingDemo {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(6, 7, 3, 8, 1);
    Double avg = numbers.stream().collect(Collectors.averagingInt(Integer::intValue));
    System.out.println("Average of the list elements- " + avg);
  }
}
Output
Average of the list elements- 5.0

Collectors.averagingLong() example

In this example we’ll get the average of List elements where List is storing longs.

public class CollectorAveragingDemo {

  public static void main(String[] args) {
    // getting list of longs
    List<Long> numbers = Arrays.asList(22L, 17L, 3L, 38L, 11L);
    Double avg = numbers.stream().collect(Collectors.averagingLong(Long::longValue));
    System.out.println("Average of the list elements- " + avg);
  }
}
Output
Average of the list elements- 18.2

Collectors.averagingDouble() example

In this example we’ll get the average of List elements where List is storing doubles.

public class CollectorAveragingDemo {

  public static void main(String[] args) {
    // getting list of longs
    List<Double> numbers = Arrays.asList(22.6, 17.4, 3.0, 57.7, 11.89);
    Double avg = numbers.stream().collect(Collectors.averagingDouble(Double::doubleValue));
    System.out.println("Average of the list elements- " + avg);
  }
}
Output
Average of the list elements- 22.518

Collectors.averaging method with custom object

In this example we’ll use averaging methods to get the average marks and average age of the students.

Student class
public class Student {
  private int rollNo;
  private String name;
  private int age;
  private double marks;
  Student(int rollNo, String name, int age, double marks){
    this.rollNo = rollNo;
    this.name = name;
    this.age = age;
    this.marks = marks;
  }
  public int getRollNo() {
    return rollNo;
  }
  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  public double getMarks() {
    return marks;
  }

  @Override
  public String toString() {
    return "Roll Number: " +  getRollNo() 
        + " Name: " + getName() + " Age: " + getAge() + " Marks: " + getMarks();
  }
}
public class CollectorAveragingDemo {
  public static void main(String[] args) {
    List<Student> studentList = Arrays.asList(new Student(1, "Mercy", 19, 75.5),
              new Student(2, "Ram", 20, 99.0),
              new Student(3, "Priscilla", 20, 68.5),
              new Student(4, "Jacques", 22, 97.0),
              new Student(5, "Peter", 19, 75.25));
    
    // To get average marks of the Students
    Double avgMarks = studentList.stream()
                   .collect(Collectors.averagingDouble(Student :: getMarks));
    
    System.out.println("Average marks of the students- " + avgMarks);
    
    // To get average age of the Students
    Double avgAge = studentList.stream()
                    .collect(Collectors.averagingInt(Student :: getAge));
    
    System.out.println("Average age of the students- " + avgAge);
  }
}
Output
Average marks of the students- 83.05
Average age of the students- 20.0

That's all for the topic Java Stream Collectors averagingInt(), averagingLong(), averagingDouble(). If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment