Saturday, August 25, 2018

Java8 - java.util.LongSummaryStatistics class example

In my previous post, we covered the Summary Statistics introduction, IntSummaryStatistics, and DoubleSummaryStatistics
In this blog post, we are going to cover LongSummaryStatistics with examples.

LongSummaryStatistics class

LongSummaryStatistics is class for collecting statistical data using like sum, max, min, average, and count operations on long data. 
Java.util package provides three classes IntSummaryStatistics for Integer data, DoubleSummaryStatistics for double data and LongSummaryStatistics for long data. These classes work with streams and lambda expressions.

LongSummaryStatistics class accept method example

This provides accept()method to record value for computation.
LongSummaryStatistics statsPrimitive = new LongSummaryStatistics();
Output is
LongSummaryStatistics{count=6, sum=98, min=2, average=16.333333, max=51}

LongSummaryStatistics Stream Array example

The below example - Created List of students using List.of() factory static method Given object streams and map this to mapToLong method which takes toLongFunction - a Functional interface which accepts the object and produces long output. finally, call summaryStatistics() method to return the object.

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

public class Summary2 {

 public static void main(String[] args) {
  List empLists = List.of(new Student(15000), new Student(2000), new Student(21000), new Student(2500));
  LongSummaryStatistics sumaryStats = -> value.getRank()).summaryStatistics();

class Student {
 long rank;

 public Student(long rank) {
  this.rank = rank;

 public long getRank() {
  return rank;
LongSummaryStatistics{count=4, sum=40500, min=2000, average=10125.000000, max=21000}

Collectors.summarizingLong() method example

Collectors.summarizingLong() is a static method defined in package.


<T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
List<Long> list = Arrays.asList(5l, 4l, 1l, 7l, 3l);
LongSummaryStatistics summaryStats =;
Output is

LongSummaryStatistics{count=5, sum=20, min=1, average=4.000000, max=7}
The above example explanation as follows.

  • Create a list from an array.
  • List to Stream using stream() reduce operation collect to with Collectors.summarizingLong parameter to return LongSummaryStatistics

LongStream summaryStatistics() method example 

LongStream is a stream version of a Long data type which supports sequential and parallel operations. summaryStatistics() method returns LongSummaryStatistics which contains summary statics of long stream data
LongStream longStream = LongStream.of(5, 4, 1, 7, 3);
LongSummaryStatistics summaryStats = longStream.summaryStatistics();
LongSummaryStatistics{count=5, sum=20, min=1, average=4.000000, max=7}
First, create a long stream using LongStream.of() method.
summaryStatistics in doublestream will collect statistics data encapsulated in an
This object has summary data like count, sum, min, average, and max of double data. This is reduction operation of Stream API and takes input array of values and gives an output of summary of data.

LongSummaryStatistics methods 

The below are in built methods supported by java for summary statistics operations on long data
Methods Description
accept(long) record the long value for summary operations. returns nothing.
Combine(LongSummaryStatistics) Combine other summary statics in this. returns nothing
getAverage() return mean operation on list of long values and returns zero if no value exists
getCount() Returns the count of list of long values recorded
getMax() returns maximum value from a list of long values. if NaN value is added, returns NaN, if no values added, returns MAX_VALUE value
getMin() returns minimum value from a long list.if NaN value is added, returns NaN, if no values added, returns MIN_VALUE value
getSum() returns of sum of all long values in list. and returns zero if no values added

Related article