Thursday, 5 May 2016

IntSummaryStatistics Example in Java 8

java.util.IntSummaryStatistics Examples

java.util.IntSummaryStatistics gives nice and handy way to do mathematical calculations in Java 8 over
primitive data types like int, double and long.

The following program demonstrates the same.
File:IntSummaryStatisticsExample.java

package codermag.net.java8;

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

public class IntSummaryStatisticsExample {
public static void main(String[] args) {
 List<Integer> integetList = Arrays.asList(98, 4, 7, 3, 2, 46, 21, 53, 17, 32, 63, 52);
 IntSummaryStatistics intStats = integetList.stream().mapToInt((x) -> x).summaryStatistics();
 
 
 System.out.println("The Maximum : "+intStats.getMax());
 System.out.println("The Minimum : "+ intStats.getMin());
 System.out.println("The Average of all the elements : "+ intStats.getAverage());
 System.out.println("No. of Elements : "+intStats.getCount());
 System.out.println("Sum of all the elements : "+intStats.getSum());
 
}
}



Output:

The Maximum : 98
The Minimum : 2
The Average of all the elements : 33.166666666666664
No. of Elements : 12
Sum of all the elements : 398