How to find average of array in java

What you will learn here about java

  • How to find average of array in java

Here we will see how to find average of array in java using java stream API. In java with the help of stream it is very easy to find average of array.

How to find average of array in java

Below sample program to find average of array in java is given below

import java.util.Arrays;

public class Avg {
    public static void main(String[] args) {
        double result = Arrays.stream(new Integer[]{15, 5, 50, 5, 25})
                .mapToDouble(Integer::intValue)
                .average()
                .getAsDouble();

        System.out.println("Average of numbers -> "+result);
    }
}

OUTPUT

Average of numbers -> 20.0

You may also like...