Calculate average of all items in a List in Java
This post will discuss how to calculate the arithmetic mean (average) of all items in a List in Java.
1. Using Stream.average() method
If you use JDK version 1.8 or above, you might want to use Stream for this trivial task. The idea is to convert the List into the corresponding primitive stream i.e, IntStream, DoubleStream, or LongStream, and call the average() method on it. It returns an optional describing the average of elements in the stream, or an empty optional if the stream is empty. Here’s the complete code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; class Main { private static double getAverage(List<Integer> list) { return list.stream() .mapToInt(a -> a) .average().orElse(0); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); double avg = getAverage(list); System.out.println(avg); // 3.0 } } |
2. Using SummaryStatistics
Another plausible way in Java 8 is to get SummaryStatistics of the corresponding primitive stream, which provides statistics such as count, min, max, sum, and average about the elements of the stream.
The following example demonstrates its usage to obtain the arithmetic mean of elements of the stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; class Main { private static double getAverage(List<Integer> list) { IntSummaryStatistics stats = list.stream() .mapToInt(Integer::intValue) .summaryStatistics(); return stats.getAverage(); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); double avg = getAverage(list); System.out.println(avg); // 3.0 } } |
Here’s equivalent code without converting the list to a primitive stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; class Main { private static double getAverage(List<Integer> list) { IntSummaryStatistics stats = list.stream() .collect(Collectors.summarizingInt(Integer::intValue)); return stats.getAverage(); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); double avg = getAverage(list); System.out.println(avg); // 3.0 } } |
3. Using Guava
If you prefer the Guava library, you can use Stats class which is a bundle of statistical summary values like sum, count, mean, min, and max, etc. To calculate the arithmetic mean of the list, you can use the static Stats.meanOf() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.math.Stats; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); double avg = Stats.meanOf(list); System.out.println(avg); // 3.0 } } |
4. Using for loop
If you’re on older versions of Java (Java 7 and before) and don’t prefer third-party libraries, you can write your custom routine for this simple task using a simple for-loop:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; class Main { private static double getAverage(List<Integer> list) { long sum = 0; for (int i: list) { sum += i; } return list.size() > 0 ? (double) sum / list.size() : 0; } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); double avg = getAverage(list); System.out.println(avg); // 3.0 } } |
That’s all about calculating the average of all items in a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)