首页 > 其他分享 >stream用法记录

stream用法记录

时间:2022-11-30 09:58:38浏览次数:60  
标签:stream 记录 students 用法 collect Student 90 学生

转自:https://blog.csdn.net/sc179/article/details/126283897

Java Stream类常见用法

目录

 

1 基本过滤:返回学生列表中90分以上的

List<Student> above90List = students.stream()
	.filter(t->t.getScore()>90)
	.collect(Collectors.toList());
  1. 通过stream()得到一个Stream对象;
  2. 调用Stream的方法filter()过滤得到90分以上的,它的返回值依然是一个Stream;
  3. 为了转换为List,调用collect方法并传递Collectors.toList(),表示将结果收集到一个List中。

2 基本转换:根据学生列表返回名称列表

List<String> nameList = students.stream()
	.map(Student::getName)
	.collect(Collectors.toList());
  • 这里使用了Stream的map函数,它的参数是一个Function函数式接口,这里传递了方法引用。

3 基本过滤和基本转换组合:返回90分以上的学生名称列表

List<String> above90Names = students.stream()
	.filter(t->t.getScore()>90)
	.map(Student::getName)
	.collect(Collectors.toList());
 
  1. filter()和map()都需要对流中的每个元素操作一次,一起使用会不会就需要遍历两次呢?答案是否定的,只需要一次。实际上,调用filter()和map()都不会执行任何实际的操作,它们只是在构建操作的流水线,调用collect才会触发实际的遍历执行,在一次遍历中完成过滤、转换以及收集结果的任务;
  2. 像filter和map这种不实际触发执行、用于构建流水线、返回Stream的操作称为中间操作(intermediate operation),而像collect这种触发实际执行、返回具体结果的操作称为终端操作(terminal operation)。

4 中间操作distinct:返回字符串列表中长度小于3的字符串、转换为小写、只保留唯一的

List<String> list = Arrays.asList(new String[]{"abc", "def", "hello", "Abc"});
List<String> retList = list.stream()
	.filter(s->s.length()<=3)
	.map(String::toLowerCase)
	.distinct()
	.collect(Collectors.toList());
 
  • 对于顺序流,内部实现时,distinct操作会使用HashSet记录出现过的元素,如果流是有顺序的,需要保留顺序,会使用LinkedHashSet。

5 中间操作sorted:过滤得到90分以上的学生,然后按分数从高到低排序,分数一样的按名称正序排序

List<Student> list = students.stream()
	.filter(t->t.getScore()>90)
	.sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getName))                
	.collect(Collectors.toList());
 
  • 这里使用了Comparator的comparing、reversed和thenComparing构建了Comparator。

5.1 过滤得到90分以上的学生,然后按分数从高到低排序,分数一样的按名称倒序排序

List<Student> list = students.stream()
	.filter(t->t.getScore()>90)
	.sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getName)).reversed()                
	.collect(Collectors.toList());

6 中间操作skip/limit:将学生列表按照分数排序,返回第3名到第5名

List<Student> list = students.stream()
	.sorted(Comparator.comparing(Student::getScore).reversed())
	.skip(2).limit(3)
	.collect(Collectors.toList());
  • skip跳过流中的n个元素,如果流中元素不足n个,返回一个空流,limit限制流的长度为maxSize;
  • skip和limit都是有状态的中间操作。对前n个元素,skip的操作就是过滤,对后面的元素,skip就是传递给流水线中的下一个操作。limit的一个特点是:它不需要处理流中的所有元素,只要处理的元素个数达到maxSize,后面的元素就不需要处理了,这种可以提前结束的操作称为短路操作。

7 中间操作mapToLong/mapToInt/mapToDouble:求学生列表的分数总和

double sum = students.stream().mapToDouble(Student::getScore).sum();

为避免装箱/拆箱,提高性能,Stream提供了返回基本类型特定流的方法。

8 终端操作max/min:返回分数最高的学生

Student student = students.stream().max(Comparator.comparing(Student::getGrade)).get();

Student student = students.stream().min(Comparator.comparing(Student::getGrade).reversed()).get();

这里假定students不为空

9 终端操作count:统计大于90分的学生个数

long above90Count = students.stream().filter(t->t.getScore()>90).count();
 

10 终端操作allMatch/anyMatch/noneMatch:判断是不是所有学生都及格了(不小于60分)

boolean allPass = students.stream().allMatch(t->t.getScore()>=60);
 
  • allMatch:只有在流中所有元素都满足条件的情况下才返回true;
  • anyMatch:只要流中有一个元素满足条件就返回true;
  • noneMatch:只有流中所有元素都不满足条件才返回true;

如果流为空,那么这几个函数的返回值都是true。

11 终端操作forEach:逐行打印大于90分的学生

students.stream().filter(t->t.getScore()>90).forEach(System.out::println);
 

12 终端操作toArray:获取90分以上的学生数组

Student[] above90Arr = students.stream().filter(t->t.getScore()>90).toArray(Student[]::new);
 

13 容器收集器toSet

toSet的使用与toList类似,只是它可以排重。toList背后的容器是ArrayList, toSet背后的容器是HashSet。

14 容器收集器toMap

toMap将元素流转换为一个Map,我们知道,Map有键和值两部分,toMap至少需要两个函数参数,一个将元素转换为键,另一个将元素转换为值。

  1. 将学生流转换为学生名称和分数的Map
Map<String, Double> nameScoreMap = students.stream().collect(Collectors.toMap(Student::getName, Student::getScore, (oldValue, value)->value));
 
  1. 转换学生流为学生id和学生对象的Map:
Map<String, Student> byIdMap = students.stream().collect(Collectors.toMap(Student::getId, Function.identity(), (oldValue, value)->value));
 
  1. 得到字符串与其长度的Map
Map<String, Integer> strLenMap = Stream.of("abc", "hello", "abc").collect(Collectors.toMap(Function.identity(), t->t.length(), (oldValue, value)->value));
 

15 分组

分组类似于数据库查询语言SQL中的group by语句,它将元素流中的每个元素分到一个组,可以针对分组再进行处理和收集。

  1. 将学生流按照年级进行分组
Map<String, List<Student>> groups = students.stream().collect(Collectors.groupingBy(Student::getGrade));
 
  1. 统计每个年级的学生个数
Map<String, Long> gradeCountMap = students.stream().collect(groupingBy(Student::getGrade, counting()));
 
  1. 统计一个单词流中每个单词的个数,按出现顺序排序
Map<String, Long> wordCountMap = Stream.of("hello", "world", "abc", "hello").collect( groupingBy(Function.identity(), LinkedHashMap::new, counting()));

  1. 对学生按年级分组,得到学生名称列表
Map<String, List<String>> gradeNameMap = students.stream().collect(groupingBy(Student::getGrade, mapping(Student::getName, toList())))

  1. 将学生按年级分组,分组内的学生按照分数由高到低进行排序
Map<String, List<Student>> gradeStudentMap = students.stream().collect(groupingBy(Student::getGrade, collectingAndSort(toList(), Comparator.comparing(Student::getScore).reversed())));
 
  1. 将学生按年级分组,分组后,每个分组只保留不及格的学生(低于60分)
Map<String, List<Student>> gradeStudentMap = students.stream()
.collect(groupingBy(Student::getGrade, collectingAndFilter(toList(), t->t.getScore()<60)));
    关注博主  

标签:stream,记录,students,用法,collect,Student,90,学生
From: https://www.cnblogs.com/sensenh/p/16937498.html

相关文章

  • Stream的 ::
    Stream的::用来取出实体类中的属性值:小例子:@OverridepublicList<AttrEntity>getRelationAttr(LongattrgroupId){List<AttrAttrgroupRelationEntity>entities......
  • 枚举小例子记录
    1、创建枚举类:packagecom.atguigu.common.constant;publicclassProductConstant{publicenumAttrEnum{ATTR_TYPE_BASE(1,"基本属性"),ATTR_TYPE_SALE......
  • 练字 行楷学习记录
    准备工作:0.7毫米中性笔1.5公分(不超过1.6)左右的格子纸,米字格-田字格-方字格-横写-竖写。执笔参考:练字前的准备(执笔)执笔要满足:握起来舒服不遮挡视线不影响长笔画书写......
  • C# luainterface luanet踩坑记录
      //C#调用dll传递c回调函数模板格式//LuaInterface.LuaDLL;using LuaInterface;publicpartialclassxxx{//......//C#默认情况下委托都是......
  • 编译Keepass 插件IIME 记录
    Keepass自动输入时,输入法总是冒出来。安装插件实现输入前自动切换为英文,自动输入完成后恢复为中文下载插件源码gitclonehttps://github.com/iuuniang/IIME.git编译......
  • jupyter notebook 踩坑记录
    1.安装软件路径不能是中文2.用户名不能是中文,否则找不到解释器#https://blog.csdn.net/weixin_51684729/article/details/124056544"""1.Win+R打开运行窗口,输入regedi......
  • using关键字在C#中的用法
    using关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型。 (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象。(一).......
  • C#--泛型委托Action<T>、Func<T>、Predicate<T>的解析和用法
    C#中的委托(Delegate)类似于C或C++中函数的指针。委托是保存对某个方法引用的一种引用类型变量。若要引用的方法,具有两个参数没有返回值,使用Action<T1, T2>委托,则不需要......
  • find记录删除的文件的方法?
    find查找某些满足条件的文件,进行删除,重点是,记录哪些文件被删除了。 方法,使用find的-print的命令,结合tee命令 #!/bin/bashdir=$(cd"$(dirname"$0")";pwd)logf......
  • Java8新特性之Stream流详细总结
    目录​​一:什么是Stream​​​​1.1简介​​​​1.2StreamAPI的特点:​​​​二 Stream流的创建​​​​2.1通过Collection接口函数​​​​2.2 通过Stream​​​​2......