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.
summarizingLong()
method?summarizingLong()
is a static method of the Collectors
, which is used to return a Collector
. This Collector
computes the summary statistics for the results that are obtained after we apply the passed ToLongFunction
implementation to the input elements.
The following statistics are provided on the elements of the stream:
The summarizingLong
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, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
ToLongFunction<? super T> mapper
: This is the function that extracts the property/attribute that needs to be summarized.This method returns the summary statistics of the elements that are returned to apply the passed ToLongFunction
implementation to the input elements.
import java.util.Arrays;import java.util.List;import java.util.LongSummaryStatistics;import java.util.stream.Collectors;import java.util.stream.Stream;public class Main {public static void main(String[] args){List<Long> longList = Arrays.asList(2343L, 2332L, 876567L);System.out.println("Contents of the list - " + longList);Stream<Long> longStream = longList.stream();LongSummaryStatistics longSummaryStatistics = longStream.collect(Collectors.summarizingLong(e -> e));System.out.println("Summary statistics of the stream - " + longSummaryStatistics);}}
long
values called longList
.longList
.longList
.longList
, using the summarizingLong
method. Here, the passed ToLongFunction
implementation returns the passed element itself.