One-liner: Extracting Long Statistics From A Collection of POJOs

From a collection of objects representing method calls:

public class MethodCall {

    private String methodName;
    private int duration;

    public MethodCall(String methodName, int duration) {
        this.methodName = methodName;
        this.duration = duration;

    public String getMethodName() {
        return methodName;

    public int getDuration() {
        return duration;

useful statistics data can be extracted with Collectors.summarizingLong:

import java.util.ArrayList;
import java.util.LongSummaryStatistics;
import static;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

public class LongSummaryStatisticsTest {

    private ArrayList<MethodCall> calls;

    public void provideTestData() {
        this.calls = new ArrayList<>();
        calls.add(new MethodCall("save", 90));
        calls.add(new MethodCall("find", 10));
        calls.add(new MethodCall("delete", 2));


    public void computeStatistics() {

        LongSummaryStatistics statistics =

        assertThat(statistics.getCount(), is(3l));
        assertThat(statistics.getMin(), is(2l));
        assertThat(statistics.getMax(), is(90l));
        assertThat(statistics.getAverage(), is(34d));
        assertThat(statistics.getSum(), is(102l));


The code above was extracted from "Effective Java EE" online course (coming soon), the second part of the Java EE Bootstrap at

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting


Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments