项目场景:
项目中对与steam流应用
1.steam通过某个字段对查询的集合分组
Map<Integer,List<Student>>map=list.stream().collect( Collectors.groupingBy(Student::getId));
2.steam判断某个集合中是否有对象包含某个元素
boolean bool = list.stream().anyMatch(company -> company.getOrgId().equals(companyLoginNum.getOrgId()));
3.steam查询集合中包含某个元素的对象
Student s= list.stream().filter(p -> p.getName().equals('张三'))
.findFirst()
.orElse(null);
4.steam通过某个字段对集合排序
List<Student> resultList = list.stream().
sorted((Comparator.comparing(Student::getAge).reversed()))
.collect(Collectors.toList());
补充说明:
通过id去对List<Student> list 分组,每组的元素如下:
Collection<List<Student>> values = map.values();for (List<Student> list1 : values){ }
本文介绍 Java Stream API 的实际应用场景,包括如何通过特定字段对集合进行分组、判断集合中是否存在包含特定元素的对象、查找包含指定元素的对象以及按字段排序等操作。

6104

被折叠的 条评论
为什么被折叠?



