Java 8 introduces an advanced concept known as Stream API. The Stream API houses multiple methods to work with the objects entering the pipeline one after the other. The stream API works on the collection of objects, ArrayList, List, etc. You can import this functionality from the “java.util” package which contains different classes, interfaces, enums methods, etc. In List or ArrayList, the number of iterations are unknown and they grow or shrink according to the storage requirement of the data. Hence, it can be challenging to determine the sum of elements of such data structures.
Quick Outline
This article provides different methods for adding the numbers with Java Streams:
- Method 1: Sum the Numbers Using the Reducer Function
- Method 2: Sum the Numbers Using the Stream and Collect() Methods
- Method 3: Sum the Numbers Using the IntStream.sum()
- Method 4: Sum the Numbers Using the MapToInt()
How to Sum the Numbers with Java Streams?
Streams work efficiently for the collections of objects, Lists, etc. Therefore, to sum the numbers, you can use the sum() method of the Java Streams. The “java.util.stream” package provides various other methods to sum the numbers in Java that are discussed as follows:
Method 1: Stream Number Using the stream.reduce()
In this code, the import statement includes all the necessary dependencies, classes, and methods from the “java.util” package. Inside the main() method, a List instance “sumNumbers” of an integer data type with the following elements is created. The integer variable “sum” stores the total sum returned by the stream.reduce() method. The reduce() method starts from 0 and sums all the numbers using the Lambda expression:
package streamSumMethod;
import java.util.*;
public class streamSumMethod {
public static void main(String[] args) {
List <Integer> sumNumbers = Arrays.asList(1 , 2 ,3, 5, 6, 10);
int sum = sumNumbers.stream().reduce(0, (a, b) -> a+b);
//adding the number
System.out.println("The sum of number is " + sum);
}
}
Output
The output is given as follows:
Method 2: Adding the Numbers Using the Stream and Collect() Method
In this code, the import statement is used to include the List, Arrays, and Collectors class from the “java.util” package. Within the main() method, the List class instance “sumNumbers” of integer data type is created and initialized with different elements. The “sum” is an integer-type variable that keeps the sum of the elements within the List using the summingInt() method. The println() method prints the value of the sum stored:
package streamSumMethod;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;
public class streamSumMethod {
public static void main(String[] args)
{
List <Integer> sumNumbers = Arrays.asList(1 , 2 ,3, 5, 6, 10);
int sum = sumNumbers.stream().collect(Collectors.summingInt(Integer :: intValue));
//adding the number
System.out.println("The sum of number is " + sum);
}
}
Output
The following screenshot indicates the output of the above-given code:
Method 3: Adding Numbers Using the IntStream.sum()
The import statement includes/imports all the essential dependencies and classes from the “java.util” package. Inside the main() method, the List class creates a “sumNumbers” instance of integer data type. The asList() method forms a List of the given elements that are then stored in the sumNumbers List. The “sum” variable is of integer data type. The “sumNumbers” invokes the stream() method. The mapToInt() method of the stream method maps each element of the List and sums them using the sum() method. The println() method then prints the value of the sum variable that contains the total of the elements of the List:
package streamSumMethod;
import java.util.*;
public class streamSumMethod {
public static void main(String[] args) {
List <Integer> sumNumbers = Arrays.asList(1 , 2 ,3, 5, 6, 20);
int sum = sumNumbers.stream().mapToInt(Integer :: intValue).sum();
//adding the number
System.out.println("The sum of number is " + sum);
}
}
Output
The output of the code is given below:
Method 4: Adding the Numbers Using the MapToInt()
The import statement includes all the dependencies, and classes such as List, Arrays, or Stream from the “java.util” package:
import java.util.*;
Inside the main() method, an object of the “sumNumbers” is created from the List class. The integer-type variable named “sum” stores the total/sum of all the elements of the ArrayList. The output is then printed on the console:
List <Integer> sumNumbers = Arrays.asList(1 , 2 ,3, 5, 6, 20);
int sum = sumNumbers.stream().mapToInt(Integer::intValue).sum();
System.out.println("Total Sum Using the Stream is = " + sum);
Output
The output is as follows:
That is all from this guide.
Conclusion
To sum the numbers using the Java Streams, use different methods such as MapToInt(), reduce(), collect(), and IntStream() as shown in this article. The Stream API works with the group of objects in the form of the pipeline. The sum() method is a method of Stream API that is imported from the “java.util” package. This article is a comprehensive guide to sum the numbers with Java Streams.