summarizing集合统计
-
作⽤:可以⼀个⽅法把统计相关的基本上都完成
-
分类
summarizingInt
summarizingLong
summarizingDouble
- 案例
# 统计学⽣的各个年龄信息
public class Main {
public static void main(String[] args) throws Exception {
List<Student> students = Arrays.asList(
new Student("广东", 23),
new Student("广东", 24),
new Student("广东", 23),
new Student("北京", 22),
new Student("北京", 20),
new Student("北京", 20),
new Student("海南", 25));
IntSummaryStatistics summaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge));
System.out.println("平均值"+summaryStatistics.getAverage());
System.out.println("人数"+summaryStatistics.getCount());
System.out.println("最大值"+summaryStatistics.getMax());
System.out.println("最小值"+summaryStatistics.getMin());
System.out.println("总和"+summaryStatistics.getSum());
}
}
// 内部类
class Student {
private int age;
private String province;
public Student(String province, int age) {
this.province = province;
this.age = age;
}
public Student() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}