在这篇博文中,我们将介绍IntSummaryStatistics类和它的方法,并举例说明。
IntSummaryStatistics是一个用于获取统计数据的类,它使用了整数数据的sum, max, min, average和count操作。IntSummaryStatistics是针对定义在java.util包中的Integer数据类型。还有其他一些类,如DoubleSummaryStatistics用于双倍数据类型,LongSummaryStatistics用于长数据类型。
创建的对象可以被设计成与java8流和lambda表达式一起工作。
我们有很多方法可以创建IntSummaryStatistics对象。
IntSummaryStatistics类接受方法示例
使用IntSummaryStatistics.accept()方法,该方法将整数值作为输入并将该值记录到对象中。
这是对整数数据进行统计运算的一种方法,它很简单
IntSummaryStatistics statsPrimitive = new IntSummaryStatistics();
statsPrimitive.accept(7);
statsPrimitive.accept(6);
statsPrimitive.accept(4);
statsPrimitive.accept(2);
statsPrimitive.accept(18);
statsPrimitive.accept(5);
System.out.println(statsPrimitive);
输出是
IntSummaryStatistics{count=6, sum=42, min=2, average=7.000000, max=18}
IntSummaryStatistics 列表流示例
首先使用List.of()方法创建了雇员对象的列表。of()方法是java9语言中引入的工厂静态方法
使用List.stream()方法为一个对象的列表创建了流。
对于每个流对象,使用mapToInt方法将其映射为ToIntFunction。ToIntFunction是一个功能接口,它将一个对象作为输入并输出整数值。请看ToIntFunction的例子。mapToInt()返回IntStream,最后调用summaryStatistics还原器操作,返回IntSummaryStatistics对象。这个对象持有封装在此对象中的统计数据。
import java.util.IntSummaryStatistics;
import java.util.List;
public class Summary2 {
public static void main(String[] args) {
List empLists = List.of(new Employee(5000), new Employee(2000), new Employee(21000),
new Employee(2500));
IntSummaryStatistics sumaryStats = empLists.stream().mapToInt((value) -> value.getSalary()).summaryStatistics();
System.out.println(sumaryStats);
}
}
class Employee {
Integer salary;
public Employee(Integer salary) {
super();
this.salary = salary;
}
public Integer getSalary() {
return salary;
}
}
上述代码执行的输出是
IntSummaryStatistics{count=4, sum=30500, min=2000, average=7625.000000, max=21000}
Collectors.summaryizingInt()是定义在java.util.stream包中的一个静态方法。语法
public static Collector summarizingInt(ToIntFunction mapper)
这个方法是一个函数,这个函数接受对象并产生整数结果作为输出。ToIntFunction是java.util.function包中提供的一个功能接口。
List list = Arrays.asList(7,8,1,5,6);
IntSummaryStatistics summaryStats = list.stream().collect(Collectors.summarizingInt(Integer::intValue));
System.out.println(summaryStats);
输出是
IntSummaryStatistics{count=5, sum=27, min=1, average=5.400000, max=8}
- 使用Arrays.asList()方法从数组中创建一个整数列表。
- 使用stream()方法创建流对象,返回对象的流。
- 最后,使用collect方法调用collect来减少操作,输入Collectors.summarizingInt
- 返回封装在IntSummaryStatistics中的计算结果
IntStream summaryStatistics()方法示例
IntStream是Stream API的一个整数版本。IntStream summaryStatistics() 提供了获取Integer Summary Statics对象的方法。
IntStream intStream = IntStream.of(8,6,4,8,3);
IntSummaryStatistics summaryStats = intStream.summaryStatistics();
System.out.println(summaryStats);
上述代码的输出是
IntSummaryStatistics{count=5, sum=29, min=3, average=5.800000, max=8}
从一个数字数组创建了IntStream IntStream*.of()方法*
通过调用IntStream.summaryStatistics方法返回了汇总统计数据。
IntSummaryStatistics方法
以下是java支持的对整数数据进行汇总统计操作的内建方法
方法名称:
说明
accept(integer):
记录用于汇总操作的整数值,不返回。
Combine(IntSummaryStatistics):
将其他总结性统计数字合并到这里,不返回。
getAverage():
返回对列表值的平均操作,如果没有值,则返回0
getCount():
返回记录的数值的数量
getMax():
返回整数值列表中的最大值。如果加入NaN值,则返回NaN,如果没有加入任何值,则返回MAX_VALUE值。
getMin():
从一个整数列表中返回最小值。如果加入NaN值,返回NaN,如果没有加入任何值,返回MIN_VALUE值。
getSum():
返回列表中所有整数值的总和。如果没有添加任何值,则返回0。