Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
summarizingdouble
collectors

What is Collectors.summarizingDouble() in Java?

abhilash

What is the Collectors class?

Collectors is a utility class which provides various implementations of reduction operations such as grouping elements, collecting elements to different collections, summarizing elements according to various criteria, and so on. The different functionalities in the Collectors class are usually used as the final operation on streams.

The summarizingDouble() method

summarizingDouble() is a static method of the Collectors class which returns a Collector. This Collector summarizes the statistics for the results that are obtained by applying the passed ToDoubleFunction implementation to the input elements.

The following statistics are provided on the elements of the stream:

  1. The count of all the elements
  2. The cumulative sum of the elements
  3. The minimum element
  4. The maximum element
  5. The average of all the elements

The summarizingDouble method is defined in the Collectors class. The Collectors class is defined in the java.util.stream package. To import the Collectors class, we will check the following import statement:

import java.util.stream.Collectors;

Syntax


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

Parameters

  • ToDoubleFunction<? super T> mapper: This is the function that is used to extract the property/attribute that needs to be summarized.

Return value

This method returns the summary statistics of the elements that are returned to apply the passed ToDoubleFunction implementation to the input elements.

Code

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

public class Main {

    public static void main(String[] args)
    {
        List<Double> doubleList = Arrays.asList(23.43, 23.32, 8.76567);

        System.out.println("Contents of the list - " + doubleList);

        Stream<Double> doubleStream = doubleList.stream();

        DoubleSummaryStatistics doubleSummaryStatistics = doubleStream.collect(Collectors.summarizingDouble(e -> e));

        System.out.println("Summary statistics of the stream - " + doubleSummaryStatistics);
    }
}
Implementation of the "summarizingDouble" method

Code explanation

  • Lines 1–4: We import the relevant packages.
  • Line 10: We define a list of double values called doubleList.
  • Line 12: We print the doubleList.
  • Line 14: We create a stream out of the doubleList.
  • Line 16: We find the average of the elements of the doubleList, using the summarizingDouble method. Here, the passed ToDoubleFunction implementation returns the passed element itself.
  • Line 18: We print the statistics obtained in line 16.

RELATED TAGS

java
communitycreator
summarizingdouble
collectors
RELATED COURSES

View all Courses

Keep Exploring