Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
long
summary
statistics
communitycreator

# What is LongSummaryStatistics in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

### Overview

For long values, the LongSummaryStatistics class is used to collect statistics such as count, min, max, total, and average. This class is intended to operate with streams, although it is not required. It was introduced in Java 8.

LongSummaryStatistics is defined in the util package in Java, so you must import the util package before you can use the LongSummaryStatistics class, as shown below.

import java.util.LongSummaryStatistics;


The statistics offered by this class are as follows:

1. Count of all elements
2. Cumulative sum of the elements
3. Minimum element
4. Maximum element
5. Average of all the elements

### Methods

Method name Description
accept(int value) Another int value is added to the summary information.
accept(long value) Another long value is added to the summary information.
combine(LongSummaryStatistics other) The state of another LongSummaryStatistics object is merged using this method
getCount() Returns the total number of values that have been seen so far.
getSum() Returns the sum of all values observed so far, or zero if none have been seen
getMin() Returns the smallest recorded long value
getMax() Returns the largest recorded long value
getAverage() Returns the arithmetic mean of all values seen so far, or zero if none have been seen.

### Examples

In the code below, we compute the statistics from a list of Long values. The accept method is used to take the long value into the summary information.

import java.util.ArrayList;
import java.util.List;
import java.util.LongSummaryStatistics;

public class Main {

public static void main(String[] args)
{
List<Long> longList = new ArrayList<>();

LongSummaryStatistics longSummaryStatistics = new LongSummaryStatistics();
for(long d: longList) longSummaryStatistics.accept(d);

System.out.println("Count of the elements - " + longSummaryStatistics.getCount());
System.out.println("Sum of the elements - " + longSummaryStatistics.getSum());
System.out.println("Minimum element - " + longSummaryStatistics.getMin());
System.out.println("Maximum element - " + longSummaryStatistics.getMax());
System.out.println("Average of the elements - " + longSummaryStatistics.getAverage());

}
}

In the code below, we use streams to get summary statistics. Thecollect() method and Collectors.summarizingInt are used to collect the statistics.

import java.util.ArrayList;import java.util.List;import java.util.LongSummaryStatistics;import java.util.stream.Collectors;public class Main {    public static void main(String[] args)    {        List<Long> longList = new ArrayList<>();        longList.add(154L);        longList.add(1234542L);        longList.add(456L);        longList.add(765L);        longList.add(23456L);        longList.add(34565432L);        LongSummaryStatistics longSummaryStatistics = longList.stream().collect(Collectors.summarizingLong(e -> e));        System.out.println("Count of the elements - " + longSummaryStatistics.getCount());        System.out.println("Sum of the elements - " + longSummaryStatistics.getSum());        System.out.println("Minimum element - " + longSummaryStatistics.getMin());        System.out.println("Maximum element - " + longSummaryStatistics.getMax());        System.out.println("Average of the elements - " + longSummaryStatistics.getAverage());    }}

RELATED TAGS

java
long
summary
statistics
communitycreator

CONTRIBUTOR

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 