How to sum a List of integers using Java 8 Stream

In this section, we will show you how to sum a List of integers using Java 8 Stream.

Following ways can be used to sum a List of integers:

1. By using mapToInt() method 
2. By using summarizingInt() method
3. By using reduce() method


Example 1: By using mapToInt () method

This mapToInt () method is an intermediate operation which returns an IntStream consisting of the results of applying the given function to the elements of this stream.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of integers
List<Integer> numberList =
new ArrayList<Integer>(Arrays.asList(5, 2, 3, 4, 9));

//Using mapToInt
int sum = numberList.stream().mapToInt(Integer::intValue).sum();
System.out.println("Sum of integers => "+sum);
}

}

Console Output:
Sum of integers => 23

Example 2: By using summarizingInt() method

The summarizingInt () is a static method of the Collectors class that is used to return the summary statistics of the results obtained from applying the passed ToIntFunction implementation to a set of input elements.
ToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces an int-valued result.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of integers
List<Integer> numberList =
new ArrayList<Integer>(Arrays.asList(5, 2, 3, 4, 9));

//Using summarizingInt
long sum = numberList.stream().
collect(Collectors.
summarizingInt(Integer::intValue)).getSum();
System.out.println("Sum of integers => "+sum);
}
}

Console Output:
Sum of integers => 23

Example 3: By using reduce() method

In Java 8, the Stream.reduce() combine elements of a stream and produces a single value.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

// Driver code
public static void main(String[] args)
{
//Creating List of integers
List<Integer> numberList =
new ArrayList<Integer>(Arrays.asList(5, 2, 3, 4, 9));

//Use reduce
int sum = numberList.stream().
reduce(Integer::sum).get().intValue();
System.out.println("Sum of integers => "+sum);
}
}

Console Output:
Sum of integers => 23

More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete