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.
summarizingDouble()
methodsummarizingDouble()
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:
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;
public static <T> Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
ToDoubleFunction<? super T> mapper
: This is the function that is used to extract the property/attribute that needs to be summarized.This method returns the summary statistics of the elements that are returned to apply the passed ToDoubleFunction
implementation to the input elements.
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); } }
double
values called doubleList
.doubleList
.doubleList
.doubleList
, using the summarizingDouble
method. Here, the passed ToDoubleFunction
implementation returns the passed element itself.RELATED TAGS
CONTRIBUTOR
View all Courses