How to Get the Sum of an Array in Java

Mohammad Irfan Feb 02, 2024
  1. Find the Sum of an Array by Using a for Loop in Java
  2. Find the Sum of an Array by Using the Stream Method in Java
  3. Find the Sum of an Array by Using the reduce Method in Java
  4. Find the Sum of an Array by Using the sum Method in Java
  5. Find the Sum of an Array by Using the IntStream Interface in Java
  6. Find the Sum of an Array by Using a Compact for Loop in Java
How to Get the Sum of an Array in Java

This tutorial introduces how to find the sum of an array in Java also lists some example codes to understand the topic.

An array is defined as a collection of similar types of elements in Java. In this article, we’ll find the sum of array elements by using some built-in methods and custom codes.

Performing this operation is very common during programming. Unfortunately, Java does not provide any specific method to get the sum of an array. So, we will use some tricks to solve this issue!

Find the Sum of an Array by Using a for Loop in Java

In this example, we used a loop to traverse each array element and get thir sum parallel. This method has a simple code that requires a single loop to get the sum. Here’s the example program:

public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
      sum += arr[i];
    }
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Find the Sum of an Array by Using the Stream Method in Java

In this example, we used the stream() method of the Arrays class and the parallel() method to get the sum of the array elements. We passed the lambda expression to the reduce() method that actually does the sum operation. See the example below:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum = Arrays.stream(arr).parallel().reduce(0, (a, b) -> a + b);
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Find the Sum of an Array by Using the reduce Method in Java

In this example, we used the reduced() method directly with the stream of arrays and get the sum of the elements. Here’s how to do it:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum = Arrays.stream(arr).reduce(0, (a, b) -> a + b);
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Find the Sum of an Array by Using the sum Method in Java

Java provides the sum() method in the Stream API to get a sum of stream sequences. Here, we passed an array to the stream and got its sum by using the sum() method. See the example below:

import java.util.Arrays;
public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum = Arrays.stream(arr).sum();
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Find the Sum of an Array by Using the IntStream Interface in Java

This method is another solution where you can use the Intsream interface to create a stream of array elements and utilize the sum() method to get the sum in a straightforward, single-line solution. Follow the sample code here:

import java.util.stream.IntStream;
public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum = IntStream.of(arr).sum();
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Find the Sum of an Array by Using a Compact for Loop in Java

In this example, we used a for loop to get the sum of array elements with an additional unique process. Here, rather than creating a loop body, we just bind up into the loop signature part. We can call it a compact loop solution. You can try it if you’re not afraid of a messy code block.

public class SimpleTesting {
  public static void main(String[] args) {
    int arr[] = new int[] {12, 34, 45, 21, 33, 4};
    int sum, i;
    for (sum = 0, i = arr.length - 1; 0 <= i; sum += arr[i--])
      ;
    System.out.println("Array Sum = " + sum);
  }
}

Output:

Array Sum = 149

Related Article - Java Array