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,

Comments

Popular posts from this blog

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

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

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

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example