lambda对list的相关操作

1、分组:Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getClassName))

解释:对studentList按照班级分组,结果是一个map<List>,map中的key为className,List为当前班级下的student

2、过滤空对象:List<xxx> list = list.stream().filter(Objects::nonNull).collect(Collectors.toList())

3、过滤空属性:List<xxx> list = list.stream().filter(s -> !StringUitls.isEmpty(s.getName())).collect(Collectors.toList())

4、过滤重复值:List<xxx> list = list.stream().distinct.collect(Collectors.toList())

5、转换list或map:https://blog.csdn.net/Txx318026/article/details/100918375

6、抽取某个属性的不重复的所有值:list.stream().map(Student::getName).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList())

7、遍历&赋值:list.stream().forEach(list -> {xx.setxxx("xxx");})

posted @ 2021-02-26 16:54  Caesar_the_great  阅读(237)  评论(0编辑  收藏  举报