java8实现分组求和,平均等数据统计

更新时间:2023-06-18 01:10:25 阅读: 评论:0

java8实现分组求和,平均等数据统计java8 实现分组聚合,平均等数据统计
写在前⾯
⼀、分组实现
1.1、简单分组
Map<String, Long> departmentNames = departmentDbs.stream()
普通高中课程标准实验教科书英语1必修
.Map(
Department::getDepartmentName,
Department::getDepartmentId)
);
1.2、集合分组
/**
* 分组函数使⽤
* 这个⽰例返回,分组后的集合对象
fade away*/察觉的意思
@Test
public void t12_2(){
List<Person> roster = Arrays.asList(
new Person("as", w(), Person.Sex.FEMALE,"Test@",12),无所谓英语>hang in there
new Person("zx", w(), Person.Sex.FEMALE,"Test@",12),decline用法
new Person("zc", w(), Person.Sex.MALE,"Test@",12),
new Person("zv", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zb", w(), Person.Sex.MALE,"Test@",12),
new Person("zn", w(), Person.Sex.FEMALE,"Test@",12),
读书感言new Person("zm", w(), Person.Sex.MALE,"Test@",12)
);
Map<Person.Sex, List<Person>> collect = roster.stream()
.collect(
);
collect.forEach((k,v)-> System.out.println(k +" : "+ v));
}
1.3、分组求和
/**
* 分组函数并求和
* MALE : 36
* FEMALE : 48
*/
@Test
public void t14(){
List<Person> roster = Arrays.asList(cookie是什么意思
new Person("as", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zx", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zc", w(), Person.Sex.MALE,"Test@",12),
new Person("zv", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zb", w(), Person.Sex.MALE,"Test@",12),
new Person("zn", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zm", w(), Person.Sex.MALE,"Test@",12) );
Map<Person.Sex, Integer> totalAgeByGender =
roster.stream()
.collect(
doubtsPerson::getGender,
0,
Person::getAge,
Integer::sum))
);
totalAgeByGender.forEach((k,v)-> System.out.println(k +" : "+ v));
}
1.4、分组求平均
英语四级作文常用句型/**
* 分组求平均
* MALE : 12.0
* FEMALE : 12.0
*/
@Test
public void t15(){
List<Person> roster = Arrays.asList(
new Person("as", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zx", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zc", w(), Person.Sex.MALE,"Test@",12),
new Person("zv", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zb", w(), Person.Sex.MALE,"Test@",12),
new Person("zn", w(), Person.Sex.FEMALE,"Test@",12),
new Person("zm", w(), Person.Sex.MALE,"Test@",12) );
Map<Person.Sex, Double> averageAgeByGender = roster.stream() .collect(
Person::getGender,
Collectors.averagingInt(Person::getAge)));
averageAgeByGender.forEach((k,v)-> System.out.println(k +" : "+ v));
}

本文发布于:2023-06-18 01:10:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/148790.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:分组   实现   教科书
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图