GeeksforGeeks App
Open App
Browser
Continue

# Java.util.LongSummaryStatistics class with Examples

The LongSummaryStatistics class is present in java.util package. It takes a collection of Long objects and is useful in the circumstances when we are dealing with a stream of large integers. It maintains a count of the number of values it has processed, their sum and various other statistics. The class can also be used with Streams.

It is useful in the sense that it maintains a running sum, average, etc. of the values and hence can be used in the manipulation of statistical data.

Class Hierarchy

```java.lang.Object
↳ java.util.LongSummaryStatistics
```

Constructors

1. LongSummaryStatistics(): A default constructor which initializes the count and sum to zero, and sets max to Long.MIN_VALUE and min to Long.MAX_VALUE.

```public LongSummaryStatistics()
```

Methods:

1. accept(): The function is overloaded and it adds the passed integer or long value into the statistical data.

```public void accept(int value)
public void accept(long value)
```
2. combine(): The function combines the statistical data of the passed LongSummaryStatistics object with the current statistical data.

```public void combine(LongSummaryStatistics other)
```
3. getCount(): Returns the count of the number of long values processed.

```public final long getCount()
```
4. getSum(): Returns the sum of all the long values processed.

```public final long getSum()
```
5. getAverage(): Returns the average of all the long values processed.

```public final double getAverage()
```
6. getMin() : Returns the minimum integer of all the long values processed.

```public final long getMin()
```
7. getMax(): Returns the maximum integer of all the long values processed.

```public final long getMax()
```
8. toString(): Returns the string representation of all the statistical data contained in the object.

```public String toString()
```
9. Example to demonstrate LongSummaryStatistics in action.

 `// Java program to demonstrate``// LongSummaryStatistics class`` ` `import` `java.util.*;`` ` `public` `class` `LongSummaryStatisticsDemo {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``LongSummaryStatistics longSummaryStatistics``            ``= ``new` `LongSummaryStatistics();``        ``List list = ``new` `LinkedList<>();``        ``list.add(100l);``        ``list.add(200l);``        ``list.add(300l);``        ``list.add(400l);``        ``list.add(500l);``        ``list.add(600l);``        ``list.add(700l);``        ``list.add(800l);``        ``list.add(900l);``        ``list.add(1000l);`` ` `        ``Iterator iterator = list.listIterator();``        ``while` `(iterator.hasNext()) {``            ``// Add the values to the LongSummaryStatistics object``            ``longSummaryStatistics.accept(iterator.next());``        ``}`` ` `        ``// Use various methods to obtain the data``        ``System.out.println(``"The count of values is "``                           ``+ longSummaryStatistics.getCount());``        ``System.out.println(``"The average of values is "``                           ``+ longSummaryStatistics.getAverage());``        ``System.out.println(``"The sum of values is "``                           ``+ longSummaryStatistics.getSum());``        ``System.out.println(``"The maximum of values is "``                           ``+ longSummaryStatistics.getMax());``        ``System.out.println(``"The minimum of values is "``                           ``+ longSummaryStatistics.getMin());``        ``System.out.println(``"The string representation is "``);``        ``System.out.println(longSummaryStatistics.toString());``    ``}``}`

Output:

```The count of values is 10
The average of values is 550.0
The sum of values is 5500
The maximum of values is 1000
The minimum of values is 100
The string representation is
LongSummaryStatistics{count=10, sum=5500, min=100, average=550.000000, max=1000}
```

My Personal Notes arrow_drop_up
Related Tutorials