Java8 - java.util.LongSummaryStatistics class example

In my previous post, we covered the Summary Statistics introduction, IntSummaryStatistics, and DoubleSummaryStatistics It covers LongSummaryStatistics with examples.

What is LongSummaryStatistics class in java8?

LongSummaryStatistics is a class for collecting statistical data using 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

It provides accept() method to record value for computation.

import java.util.LongSummaryStatistics;

public class LongSummaryStatisticsExample {
    public static void main(String[] args) {
        LongSummaryStatistics statsPrimitive = new LongSummaryStatistics();
        statsPrimitive.accept(17);
        statsPrimitive.accept(6);
        statsPrimitive.accept(4);
        statsPrimitive.accept(2);
        statsPrimitive.accept(18);
        statsPrimitive.accept(51);
        System.out.println(statsPrimitive);
    }
}

Output:

LongSummaryStatistics{count=6, sum=98, min=2, average=16.333333, max=51}

LongSummaryStatistics Stream Array lambda expression example

The below example -

  • Created a List of students using the 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 the 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 = empLists.stream().mapToLong((value) -> value.getRank()).summaryStatistics();
        System.out.println(sumaryStats);
    }
}

class Student {
    long rank;

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

    public long getRank() {
        return rank;
    }
}

Output:

LongSummaryStatistics{count=4, sum=40500, min=2000, average=10125.000000, max=21000}

Collectors summarizingLong() method example

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

Syntax:

<T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)

Here is an example

import java.util.Arrays;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;

public class LongSummaryStatisticsExample {

    public static void main(String[] args) {
        List<Long> list = Arrays.asList(5l, 4l, 1l, 7l, 3l);
        LongSummaryStatistics summaryStats = list.stream().collect(Collectors.summarizingLong(Long::longValue));
        System.out.println(summaryStats);

    }
}

Output is

LongSummaryStatistics{count=5, sum=20, min=1, average=4.000000, max=7}

The above example explanation is 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 that supports sequential and parallel operations.

summaryStatistics() method returns LongSummaryStatistics which contains summary statics of long stream data.

import java.util.LongSummaryStatistics;
import java.util.stream.LongStream;

public class LongSummaryStatisticsExample {

    public static void main(String[] args) {
        LongStream longStream = LongStream.of(5, 4, 1, 7, 3);
        LongSummaryStatistics summaryStats = longStream.summaryStatistics();
        System.out.println(summaryStats);

    }
}

Output:

LongSummaryStatistics{count=5, sum=20, min=1, average=4.000000, max=7}

Following are sequential steps for the above program.

  • First, create a long stream using the LongStream.of() method.
  • summaryStatistics in doubleStream will collect statistics data encapsulated in an object.
  • This object has summary data like count, sum, min, average, and a max of double data.
  • This is a reduction operation of Stream API and takes an 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

MethodsDescription
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

Conclusion

Learned LongSummaryStatistics object creation and methods usage with example