How to use Collectors.summarizingDouble() in Java 8

37 total views, 1 views today

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

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

Collectors.summarizingDouble()

Collectors.summarizingDouble() in Java 8 is a useful and wonderful operation of Stream collect(). It provides us the statistics of the stream of elements. It returns a class that gives us all necessary statistical information such as average, sum, count, min and max.

Collectors.summarizingDouble() is similar to summarizingInt() but it returns a class which will have stats info in Double type.It is the Double type version of summarizingInt.

Collectors.summarizingDouble() returns a class called DoubleSummaryStatistics.

Collectors.summarizingDouble() syntax

public static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)

As per documentation, Returns a Collector which applies a double-producing mapping function to each input element, and returns summary statistics for the resulting values.

Collectors.summarizingDouble() example

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

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

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

        DoubleSummaryStatistics stats = list.stream().collect(Collectors.summarizingDouble(i -> i));
        System.out.println("Average : " + stats.getAverage());
        System.out.println("Count : " + stats.getCount());
        System.out.println("Min : " + stats.getMin());
        System.out.println("Max : " + stats.getMax());
        System.out.println("Sum : " + stats.getSum());
    }
}

Output

Average : 7.428571428571429
Count : 7
Min : 1.0
Max : 17.0
Sum : 52.0

 

References : summarizingDouble()

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.