Blog Detail

  • Home
  • Summary Statistics

Summary Statistics

Sample program for get the min, max and average from collection of numbers using java 8 Summary Statistics.

Java 8 provide various class based on data types for get the statistics those are below

  • IntSummaryStatistics
  • LongSummaryStatistics
  • DoubleSummaryStatistics
package com.samplecoder.streams;

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

public class SummaryStatistics {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Integer> intNumbers = Arrays.asList(100,3,200,2,1,23,43,545,232);
		
		IntSummaryStatistics intStatistics = intNumbers.stream().collect(Collectors.summarizingInt(item -> item));
		
		System.out.println("Int Summary \n"+intStatistics);
		
		LongSummaryStatistics longStatistics = intNumbers.stream().collect(Collectors.summarizingLong(item -> item));
		System.out.println("Long Summary \n"+longStatistics);
		
		List<Number> doubleNumbers = Arrays.asList(100.10,3.2,200.11,2.2,1,2.3,43,545,232);
		DoubleSummaryStatistics doubleStatistics = doubleNumbers.stream().collect(Collectors.summarizingDouble(item -> item.doubleValue()));
		System.out.println("Double Summary \n"+doubleStatistics);
	}

}

Result

Int Summary 
IntSummaryStatistics{count=9, sum=1149, min=1, average=127.666667, max=545}
Long Summary 
LongSummaryStatistics{count=9, sum=1149, min=1, average=127.666667, max=545}
Double Summary 
DoubleSummaryStatistics{count=9, sum=1128.910000, min=1.000000, average=125.434444, max=545.000000}

Write a comment