In this article, we will discuss how to calculate sum and average of a List or ArrayList in Java 8
1. Java 8 – Find sum and average of a List
- Use IntSummaryStatistics to find various parameters/statistics of a List like,
- Sum of all elements in a List using getSum() method which returns value in long-type
- Average of all elements in a List using getAverage() method which returns value in double-type
- Minimum element from a List using getMin() method which returns value in integer-type
- Maximum element from a List using getMax() method which returns value in integer-type
- Number of elements in a List (or count) using getCount() method which returns value in long-type
CalculateStatisticsOfListInJava8.java
package in.bench.resources.list.sum.average;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
public class CalculateStatisticsOfListInJava8 {
public static void main(String[] args) {
// list
List<Integer> numbers = Arrays.asList(
2, 3, 5, 7, 11, 13, 17
);
// print numbers to console
System.out.println("Original elements :-");
numbers.stream().forEach(num -> System.out.println(num));
// IntSummaryStatistics using Java 8 Stream API
IntSummaryStatistics statistics = numbers
.stream()
.mapToInt(num -> num)
.summaryStatistics();
// 1. sum - print sum to the console
System.out.println("\n1. Sum is = " + statistics.getSum());
// 2. average - print average to the console
System.out.println("\n2. Average is = " + statistics.getAverage());
// 3. min - print minimum to the console
System.out.println("\n3. Minimum is = " + statistics.getMin());
// 4. max - print maximum to the console
System.out.println("\n4. Maximum is = " + statistics.getMax());
// 5. count - print count to the console
System.out.println("\n5. Count is = " + statistics.getCount());
}
}
Output:
Original elements :-
2
3
5
7
11
13
17
1. Sum is = 58
2. Average is = 8.285714285714286
3. Minimum is = 2
4. Maximum is = 17
5. Count is = 7
2. Java – Find sum and average of a List
- In the below illustrations, we will find sum, average, minimum, maximum and count of elements in a List
- Sum – by iterating using enhanced for-loop and adding/summing & saving in a variable
- Average – divide above calculated sum by number of elements (or size of elements)
- Sort List elements in ascending order
- Minimum – get 1st element from sorted List using index 0
- Maximum – get last element from sorted List using last index i.e., (size – 1)
- Use size() method of List to get number/count of elements
CalculateStatisticsOfList.java
package in.bench.resources.list.sum.average;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CalculateStatisticsOfList {
public static void main(String[] args) {
// local variables
int sum = 0;
double average = 0.0;
// list
List<Integer> numbers = Arrays.asList(
2, 3, 5, 7, 11, 13, 17
);
// print numbers to console
System.out.println("Original elements :-");
numbers.stream().forEach(num -> System.out.println(num));
// sort List elements to find min and max
Collections.sort(numbers);
// find sum by iterating using enhanced for-loop
for(int num : numbers) {
sum += num;
}
// 1. sum - print sum to the console
System.out.println("\n1. Sum is = " + sum);
// find average by dividing sum and size of List elements
average = (double) sum / (double) numbers.size();
// 2. average - print average to the console
System.out.println("\n2. Average is = " + average);
// 3. min - print minimum to the console
System.out.println("\n3. Minimum is = " + numbers.get(0));
// 4. max - print maximum to the console
System.out.println("\n4. Maximum is = " + numbers.get(numbers.size() - 1));
// 5. count - print count to the console
System.out.println("\n5. Count is = " + numbers.size());
}
}
Output:
Original elements :-
2
3
5
7
11
13
17
1. Sum is = 58
2. Average is = 8.285714285714286
3. Minimum is = 2
4. Maximum is = 17
5. Count is = 7
Related Articles:
- Java 8 – Find Largest number in an Arrays or List or Stream
- Java 8 – Find Smallest number in an Arrays or List or Stream
- Java 8 – Find 2nd Largest number in an Arrays or List or Stream
- Java 8 – Find 2nd Smallest number in an Arrays or List or Stream
- Java 8 – Find sum of Largest 2 numbers in an Arrays or List or Stream
- Java 8 – Find sum of Smallest 2 numbers in an Arrays or List or Stream
- Java 8 – Find 1st and Last elements in an Arrays
- Java 8 – Find 1st and Last elements in a List or ArrayList
- Java 8 – Find 1st and Last elements in a Set or HashSet
- Java 8 – Find 1st and Last entries in a Map or HashMap
- Java 8 – Find sum and average of a List or ArrayList
- Java 8 – How to calculate sum and average of an Arrays ?
References:
Happy Coding !!
Happy Learning !!