publicclassUserPo{privateStringname;私人双倍分数;//省略构造函数和getter,setter}filterfilter:filter,就是过滤器,合格通过,不合格过滤掉//filter成绩不为空的学生人数count=list.stream().filter(p->null!=p.getScore()).count();mapmap:映射,将原始集合映射到新集合,在VO和PO处理过程中比较常见。本例中原集合为PO集合,新集合可以自定义映射为成绩集合,也可以对新集合进行相关操作//取出所有学生的成绩ListscoreList=list.stream().map(p->p.getScore()).collect(Collectors.toList());//将学生姓名集合串成字符串,以逗号分隔StringnameString=list.stream().map(p->p.getName()).collect(Collectors.joining(","));sortedsorted:排序,可以按照指定的字段进行排序//如果按照倒序对学生进行排序对于成绩,您不需要添加.reversed()filterList=list.stream().filter(p->null!=p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());forEachforEach:这个应该是最常用的,即对每个元素进行自定义操作除了forEach操作会改变原集合的数据,其他操作都会不改变原来的集合,这点一定要注意//学生成绩太差,通过率太低,给每个学生加10分,放一个水//forEachfilterList.stream().forEach(p->p.setScore(p.getScore()+10));collectcollect:聚合,可用于GroudBy按指定字段分类,也可用于返回列表或拼凑字符串//GroupbyscoreMap>groupByScoreMap=list.stream().filter(p->null!=p.getScore()).collect(Collectors.groupingBy(UserPo::getScore));for(Map.Entry>entry:groupByScoreMap.entrySet()){System.out.println("分数:"+entry.getKey()+"人数:"+entry.getValue().size());}//returnlistListscoreList=list.stream().map(p->p.getScore()).collect(Collectors.toList());//返回字符串,用逗号分隔StringnameString=list.stream().map(p->p.getName()).collect(Collectors.joining(","));statisticsstatistics:统计,可以统计中位数、平均值、最大值和最小值DoubleSummaryStatisticsstatistics=filterList.stream().mapToDouble(p->p.getScore()).summaryStatistics();System.out.println("The列表中最大的数字:"+statistics.getMax());System.out.println("列表中最小的数:"+statistics.getMin());System.out.println("所有数的总和:"+statistics.getSum());System.out.println("Average:"+statistics.getAverage());parallelStreamparallelStream:并行流,可以使用多个Thread进行流操作,提高效率但是它没有线程传播,所以需要充分评估是否需要使用并行流操作//parallelstreamcount=list.parallelStream().filter(p->null!=p.getScore())。count();练习学生stuA=newStudent(1,"A","M",184);StudentstuB=newStudent(2,"B","G",163);StudentstuC=newStudent(3,"C","M",175);学生stuD=newStudent(4,"D","G",158);StudentstuE=newStudent(5,"A","M",158);//stream-forEach循环list.stream().forEach(stu->System.out.println("stream-forEach:"+stu.getName()));//stream-filter过滤就是执行逻辑longcount=list.stream().filter(stu->stu.height>180).count();list.stream().filter(stu->stu.height>180).forEach(stu->System.out.println("stream-filter:"+stu));//Stream-toMap为了避免key冲突,(key1,key2)->key1表示前者Mapmaps=list.stream().collect(Collectors.toMap(Student::getName,Function.identity(),(key1,key2)->key1));System.out.println("key-object"+maps);MapnewMaps=list.stream().collect(Collectors.toMap(Student::getName,Student::getHeight,(key1,key2)->key1));//分流去重+指定字段去重publicclassStreamUtil{/***指定字段去重*@paramkeyExtractor*@return*/staticPredicatedistinctByKey(FunctionkeyExtractor){Map