# Java 8 Stream- Sum of List of Integers

Java 8 `Stream` interface provides `mapToInt()` method to convert a stream integers into a `IntStream` object and upon calling `sum()` method of `IntStream`, we can calculate the sum of a list of integers.

`IntStream` is available for primitive int-valued elements supporting sequential and parallel aggregate operations.

``````int total1 = listOfInteger.stream().mapToInt(Integer::intValue).sum();
int total2 = listOfInteger.stream().mapToInt(i -> i).sum();
// Bonus point :)
int total3 = listOfInteger.stream().collect(Collectors.summingInt(Integer::intValue));``````

Let’s see the full example.

Main.java
``````package org.websparrow;

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

public class Main {

public static void main(String[] args) {

List<Integer> listOfInteger = Arrays.asList(8, 22, 02, 28);

// Java 8- method reference
int total1 = listOfInteger.stream().mapToInt(Integer::intValue).sum();
System.out.println("Total 1:" + total1);

int total2 = listOfInteger.stream().mapToInt(i -> i).sum();
System.out.println("Total 2:" + total2);

// Java 8- Collectors: Bonus point:)
int total3 = listOfInteger.stream()
.collect(Collectors.summingInt(Integer::intValue));
System.out.println("Total 3:" + total3);
}
}``````

Output

``````Total 1:60
Total 2:60
Total 3:60``````

### References

#### Similar Posts
{"ft_lang_label":"__label__en","ft_lang_prob":0.51778346,"math_prob":0.94259316,"size":1577,"snap":"2022-27-2022-33","text_gpt3_token_len":380,"char_repetition_ratio":0.1481246,"word_repetition_ratio":0.020512821,"special_character_ratio":0.25681674,"punctuation_ratio":0.24092409,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99758655,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-17T00:36:09Z\",\"WARC-Record-ID\":\"<urn:uuid:f4379451-6fb8-4285-89e8-f1bde01c58f2>\",\"Content-Length\":\"54827\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2ddacca1-fb13-4da7-99cb-00baffbacb49>\",\"WARC-Concurrent-To\":\"<urn:uuid:ee54ba9c-f063-4662-ba46-a301441f57cb>\",\"WARC-IP-Address\":\"104.21.94.185\",\"WARC-Target-URI\":\"https://websparrow.org/java/java-8-stream-sum-of-list-of-integers\",\"WARC-Payload-Digest\":\"sha1:N6ZR534L4N5QGIWCUEKXSKSVUAL3MTHE\",\"WARC-Block-Digest\":\"sha1:ZG25MFRBWW7V3KFNAFIWRO3PFWOJDY43\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882572833.78_warc_CC-MAIN-20220817001643-20220817031643-00775.warc.gz\"}"}