JAVA 8 In Action 读书笔记 (三) : Collect In Stream
来源:互联网 发布:邮票淘宝 编辑:程序博客网 时间:2024/05/22 11:31
首先定义测试对象
//POJO, Person对象public class Person { private String name; private int weight; private boolean isMale; private String education; public Person(String name, int weight, boolean isMale,String education) { super(); this.name = name; this.weight = weight; this.isMale=isMale; this.education=education; } public String getEducation() { return education; } public void setEducation(String education) { this.education = education; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override public String toString() { // TODO Auto-generated method stub return name+" : "+ weight +" KG "+(isMale?", 男":", 女")+ ", "+education; } public boolean isOverweight() { return weight>80; } public boolean isMale() { return isMale; } public void setMale(boolean isMale) { this.isMale = isMale; } @Override public boolean equals(Object target) { // TODO Auto-generated method stub return name.equals(((Person)target).getName()); } @Override public int hashCode() { // TODO Auto-generated method stub return name.hashCode(); }}
测试代码:
public class Demo { public static void main(String[] args) { List<Person> source = Arrays.asList(new Person("Li Lei", 90,true,"大学"), new Person("Li Lei", 98,true,"小学"), new Person("Han Meimei", 60,false,"中学"), new Person("Xiao wang", 82,false,"中学"), new Person("Xiao zhang", 50,false,"小学"),new Person("Xiao li", 94,false,"大学")); //原始数据打印 System.out.println("原始数据打印 : "); source.stream().forEach(System.out::println); //Li Lei : 90 KG , 男, 大学 //Li Lei : 98 KG , 男, 小学 //Han Meimei : 60 KG , 女, 中学 //Xiao wang : 82 KG , 女, 中学 //Xiao zhang : 50 KG , 女, 小学 //Xiao li : 94 KG , 女, 大学 //样本总数 long totalSize= source.stream().collect(Collectors.counting()); System.out.println("样本总数 : "+totalSize); //样本总数 : 6 //体重最重最轻的的 System.out.println("最重的 : "); source.stream().collect(Collectors.maxBy(Comparator.comparingInt(Person::getWeight))).ifPresent(System.out::println); //Li Lei : 98 KG , 男, 小学 System.out.println("最轻的 : "); source.stream().collect(Collectors.minBy(Comparator.comparingInt(Person::getWeight))).ifPresent(System.out::println); //Xiao zhang : 50 KG , 女, 小学 System.out.println("最重的女孩 : "); source.stream().filter((person)->!person.isMale()).collect(Collectors.maxBy(Comparator.comparingInt(Person::getWeight))).ifPresent(System.out::println); //Xiao li : 94 KG , 女, 大学 //体重总量 int totalWeight=source.stream().collect(Collectors.summingInt(Person::getWeight)); System.out.println("体重总数 : "+totalWeight+" KG"); //体重总数 : 474 KG //另外一种方法求体重总量 int totalWeight2=source.stream().collect(Collectors.reducing(0,Person::getWeight,(i,j)->i+j)); System.out.println("体重总数2 : "+totalWeight2+" KG"); //体重总数2 : 474 KG //平均重量 double avarageWeight=source.stream().collect(Collectors.averagingInt(Person::getWeight)); System.out.println("平均体重 : "+avarageWeight+" KG"); //平均体重 : 79.0 KG //汇总数据 IntSummaryStatistics summary=source.stream().collect(Collectors.summarizingInt(Person::getWeight)); System.out.println("汇总数据: "+summary); //汇总数据: IntSummaryStatistics{count=6, sum=474, min=50, average=79.000000, max=98} //名字列表 String nameList= source.stream().map(Person::getName).distinct().collect(Collectors.joining(",")); System.out.println("名字列表: "+nameList); //名字列表: Li Lei,Han Meimei,Xiao wang,Xiao zhang,Xiao li //按教育背景分组 System.out.println("按教育背景分组: "); Map<String,List<Person>> personsByGender = source.stream().collect(Collectors.groupingBy(Person::getEducation)); System.out.println(personsByGender); //{中学=[Han Meimei : 60 KG , 女, 中学, Xiao wang : 82 KG , 女, 中学], 大学=[Li Lei : 90 KG , 男, 大学, Xiao li : 94 KG , 女, 大学], 小学=[Li Lei : 98 KG , 男, 小学, Xiao zhang : 50 KG , 女, 小学]} //二级分组,先教育背景,再体重 System.out.println("二级分组,先教育背景,再体重: "); Map<String,Map<Boolean,List<Person>>> personsByGenderAndWeight = source.stream().collect(Collectors.groupingBy(Person::getEducation, Collectors.groupingBy(Person::isOverweight))); System.out.println(personsByGenderAndWeight); //{中学={false=[Han Meimei : 60 KG , 女, 中学], true=[Xiao wang : 82 KG , 女, 中学]}, 大学={true=[Li Lei : 90 KG , 男, 大学, Xiao li : 94 KG , 女, 大学]}, 小学={false=[Xiao zhang : 50 KG , 女, 小学], true=[Li Lei : 98 KG , 男, 小学]}} //统计分组人数 System.out.println("按教育背景分组并统计人数 : "); Map<String,Long> personsByGenderCount = source.stream().collect(Collectors.groupingBy(Person::getEducation,Collectors.counting())); System.out.println(personsByGenderCount); //{中学=2, 大学=2, 小学=2} System.out.println("二级分组,先教育背景,再体重,并统计人数: "); Map<String,Map<Boolean,Long>> personsByGenderAndWeightCount = source.stream().collect(Collectors.groupingBy(Person::getEducation, Collectors.groupingBy(Person::isOverweight,Collectors.counting()))); System.out.println(personsByGenderAndWeightCount); //{中学={false=1, true=1}, 大学={true=2}, 小学={false=1, true=1}} //不同教育背景下最重的 System.out.println("不同教育背景下最重的:"); Map<String,Optional<Person>> personsByGenderAndWeightMax= source.stream().collect(Collectors.groupingBy(Person::getEducation,Collectors.maxBy(Comparator.comparing(Person::getWeight)))); System.out.println(personsByGenderAndWeightMax); //{中学=Optional[Xiao wang : 82 KG , 女, 中学], 大学=Optional[Xiao li : 94 KG , 女, 大学], 小学=Optional[Li Lei : 98 KG , 男, 小学]} //分区 System.out.println("根据性别分区:"); Map<Boolean,List<Person>> partitionPerson=source.stream().collect(Collectors.partitioningBy(Person::isMale)); System.out.println(partitionPerson); //{false=[Han Meimei : 60 KG , 女, 中学, Xiao wang : 82 KG , 女, 中学, Xiao zhang : 50 KG , 女, 小学, Xiao li : 94 KG , 女, 大学], true=[Li Lei : 90 KG , 男, 大学, Li Lei : 98 KG , 男, 小学]}}
完整运行结果:
原始数据打印 :
Li Lei : 90 KG , 男, 大学
Li Lei : 98 KG , 男, 小学
Han Meimei : 60 KG , 女, 中学
Xiao wang : 82 KG , 女, 中学
Xiao zhang : 50 KG , 女, 小学
Xiao li : 94 KG , 女, 大学
样本总数 : 6
最重的 :
Li Lei : 98 KG , 男, 小学
最轻的 :
Xiao zhang : 50 KG , 女, 小学
最重的女孩 :
Xiao li : 94 KG , 女, 大学
体重总数 : 474 KG
体重总数2 : 474 KG
平均体重 : 79.0 KG
汇总数据: IntSummaryStatistics{count=6, sum=474, min=50, average=79.000000, max=98}
名字列表: Li Lei,Han Meimei,Xiao wang,Xiao zhang,Xiao li
按教育背景分组:
{中学=[Han Meimei : 60 KG , 女, 中学, Xiao wang : 82 KG , 女, 中学], 大学=[Li Lei : 90 KG , 男, 大学, Xiao li : 94 KG , 女, 大学], 小学=[Li Lei : 98 KG , 男, 小学, Xiao zhang : 50 KG , 女, 小学]}
二级分组,先教育背景,再体重:
{中学={false=[Han Meimei : 60 KG , 女, 中学], true=[Xiao wang : 82 KG , 女, 中学]}, 大学={true=[Li Lei : 90 KG , 男, 大学, Xiao li : 94 KG , 女, 大学]}, 小学={false=[Xiao zhang : 50 KG , 女, 小学], true=[Li Lei : 98 KG , 男, 小学]}}
按教育背景分组并统计人数 :
{中学=2, 大学=2, 小学=2}
二级分组,先教育背景,再体重,并统计人数:
{中学={false=1, true=1}, 大学={true=2}, 小学={false=1, true=1}}
不同教育背景下最重的:
{中学=Optional[Xiao wang : 82 KG , 女, 中学], 大学=Optional[Xiao li : 94 KG , 女, 大学], 小学=Optional[Li Lei : 98 KG , 男, 小学]}
根据性别分区:
{false=[Han Meimei : 60 KG , 女, 中学, Xiao wang : 82 KG , 女, 中学, Xiao zhang : 50 KG , 女, 小学, Xiao li : 94 KG , 女, 大学], true=[Li Lei : 90 KG , 男, 大学, Li Lei : 98 KG , 男, 小学]}
Github源码
Eclipse工程源码
- JAVA 8 In Action 读书笔记 (三) : Collect In Stream
- JAVA 8 In Action 读书笔记 (二) : Stream
- Java 8 in Action (2) -- Stream 流
- JAVA 8 In Action 读书笔记 (一) : Lambda
- JAVA 8 In Action 读书笔记 (四) : LocalDateTime , Instant
- JAVA 8 In Action 读书笔记 (五) : MultiThreads
- 读书笔记Java Reflection In Action 157页
- struts in action读书笔记
- Ajax in action读书笔记
- Spring in action 读书笔记
- jQuery in Action读书笔记
- 读书笔记:spring in action
- 《Lucene In Action》读书笔记
- maven in action读书笔记
- junit in action读书笔记
- spring in action 读书笔记
- Spring In Action读书笔记
- spring in action读书笔记
- 1127: 矩阵乘积
- 娓娓道来c指针 (1)指针就是地址
- tp3.2 setInc返回值是
- Eclipse中下载并使用Mybatis
- 领英:经济图谱在中国,为人才就业勾画新版图
- JAVA 8 In Action 读书笔记 (三) : Collect In Stream
- 1128: 课程平均分
- oracle查询一段时间内的数据
- matlab常用函数
- IDEA下SSH项目的配置(一)-配置strut2
- andriod 资源大全
- win10下安装Ubuntu遇到的一些问题
- 微信小程序 网络请求之设置合法域名
- 娓娓道来c指针 (3)指针和数组