Java – List Statistics

Java 8 provides classes called IntSummaryStatistics, DoubleSummaryStatistics and LongSummaryStatistics which give a state object for collecting statistics such as count, min, max, sum, and average.

 

package com.pwn.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

public class NumericalStreamsStastics {

	public static void main(String[] args) {

		List<Integer> naturalNumbers = Arrays.asList(1, 2, 3, 4, 5, 6,
				7, 8, 9, 10);
		IntSummaryStatistics stats = naturalNumbers.stream()
				.mapToInt((x) -> x).summaryStatistics();
		System.out.println(stats);

	}

}
IntSummaryStatistics{count=10, sum=55, min=1, average=5.500000, max=10}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s