Collectors.summingInt()/summingLong()/summingDouble()

35 total views, 1 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to use Collectors.summingInt()/summingLong()/summingDouble()  in Java 8.

If you would like to learn about Java 8 Stream operations.Please check out this link.

Collectors.summingInt()/summingLong()/summingDouble()  is used in Java 8 streams to sum the elements of the Stream into a single value. Each operation produces the sum value of the stream elements.

Let us now get started in learning each operation.

Collectors.summingInt()

Collectors.summingInt() allows us to simply sum all the elements in the stream and return an integer.

Syntax

summingInt(ToIntFunction<? super T> mapper)

Returns a Collector that produces the sum of an integer-valued function applied to the input elements.

Collectors.summingInt() example

In the below example, we are summing all integer value in the stream and returning a result.

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

public class StaticReference
{
    public static void main(String[] args)
    {

        List<Integer> fruits = Arrays.asList(1, 2, 3, 15, 6, 17, 8);

        Integer sumInt = fruits.stream().collect(Collectors.summingInt(i -> i));
        System.out.println(sumInt);
    }
}

Output : 52

Collectors.summingLong()

Collectors.summingLong() allows us to sum all the elements in the stream and return a Long.

Syntax

summingLong(ToLongFunction<? super T> mapper)

Returns a Collector that produces the sum of a long-valued function applied to the input elements.

Collectors.summingLong() example

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

public class StaticReference
{
    public static void main(String[] args)
    {

        List<Integer> fruits = Arrays.asList(1, 2, 3, 15, 6, 17, 8);

        Long sumInt = fruits.stream().collect(Collectors.summingLong(i -> i));
        System.out.println(sumInt);
    }
}

Output : 52

Collectors.summingDouble()

Collectors.summingDouble() allows us to sum all the elements in the stream and return a Double.

Syntax

summingDouble(ToDoubleFunction<? super T> mapper)

Returns a Collector that produces the sum of a double-valued function applied to the input elements.

Collectors.summingDouble() example

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

public class StaticReference
{
    public static void main(String[] args)
    {

        List<Integer> fruits = Arrays.asList(1, 2, 3, 15, 6, 17, 8);

        Double sumInt = fruits.stream().collect(Collectors.summingDouble(i -> i));
        System.out.println(sumInt);
    }
}

Output : 52.0

 

References : Official documentation

 

I hope you like this tutorial. Thanks for reading and Please leave us a comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.