Calculate sum of all elements in a List in Java
This post will discuss how to calculate the sum of all elements in a List in Java.
1. IntStream’s sum() method
A simple solution to calculate the sum of all elements in a List is to convert it into IntStream and call sum() to get the sum of elements in the stream. There are several ways to get IntStream from Stream<Integer> using mapToInt() method.
1. Using method reference Integer::intValue
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().mapToInt(Integer::intValue).sum(); System.out.println(sum); // 15 } } |
2. Using method reference Integer::valueOf
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().mapToInt(Integer::valueOf).sum(); System.out.println(sum); // 15 } } |
3. Using Lambda expression i -> i
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().mapToInt(i -> i).sum(); System.out.println(sum); // 15 } } |
2. Reduce operation
Another solution is to perform reduce operation to perform the addition.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().reduce((x, y) -> x + y).get(); System.out.println(sum); // 15 } } |
We can simplify the above code using method reference Integer::sum, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().reduce(Integer::sum).get(); System.out.println(sum); // 15 } } |
The code can be further simplified by removing Optional:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = nums.stream().reduce(0, Integer::sum); System.out.println(sum); // 15 } } |
3. Using IntSummaryStatistics
Finally, we can get IntSummaryStatistics instance from IntStream, which has getSum() method to get the sum of values in it.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); long sum = nums.stream().mapToInt(Integer::valueOf).summaryStatistics().getSum(); System.out.println(sum); // 15 } } |
4. Naive solution
We can even write a custom routine to calculate the sum of all elements in a List using a for-loop, as shown below:
|
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 { public static int getSum(List<Integer> nums) { int sum = 0; for (int i: nums) { sum += i; } return sum; } public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); int sum = getSum(nums); System.out.println(sum); // 15 } } |
That’s all about calculating the sum of all elements 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 :)