Get sum of all elements of an array in Java 8 and above
This post will discuss how to get the sum of all elements of an array in Java.
Java didn’t supply any built-in method for this simple task, unlike many other programming languages like std::accumulate in C++, array_sum() method in PHP, Enumerable.Sum method (IEnumerable<Int32>) in .NET Framework, etc., Before Java 8, the only solution is to iterate the given array using the for-each loop and accumulate the sum of all elements in a variable. This approach is demonstrated here.
With the introduction of Java 8 Stream, we can easily get a sum of all array elements using the Stream.sum() method. To get a stream of array elements, we can use the Arrays.stream() method. This method is overloaded for arrays of int, double and long type. It also has overloaded versions, which allows you to get a sum of elements between the specified indices of the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; int sum = Arrays.stream(A).sum(); System.out.println("The sum of all the array elements is " + sum); } } |
We can also call the of() method of IntStream, DoubleStream, and LongStream class for int, double and long array, respectively, to get a stream of array elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.stream.IntStream; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; int sum = IntStream.of(A).sum(); System.out.println("The sum of all the array elements is " + sum); } } |
We can also use reduction to perform the addition operation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; int sum = Arrays.stream(A).reduce((x, y) -> x + y).getAsInt(); // or use // int sum = Arrays.stream(A).reduce(0, (x, y) -> x + y); System.out.println("The sum of all the array elements is " + sum); } } |
We can simplify the above code using method references, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; int sum = Arrays.stream(A).reduce(Integer::sum).getAsInt(); System.out.println("The sum of all the array elements is " + sum); } } |
Finally, we can use summaryStatistics() to get information about the elements of this stream like min/max element, average and total sum. It returns IntSummaryStatistics object which has getSum() method to get the sum of values.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; long sum = Arrays.stream(A).summaryStatistics().getSum(); System.out.println("The sum of all the array elements is " + sum); } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.IntSummaryStatistics; class Main { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 5 }; IntSummaryStatistics stat = new IntSummaryStatistics(); for (int i : A) { stat.accept(i); } long sum = stat.getSum(); System.out.println("The sum of all the array elements is " + sum); } } |
That’s all about getting the sum of all elements of an array 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 :)