代码和注释如下:
List<TestDto> testDtoList = new ArrayList<>();
testDtoList.add(new TestDto("张三","北京",20));
testDtoList.add(new TestDto("李四","北京",35));
testDtoList.add(new TestDto("王五","北京",31));
testDtoList.add(new TestDto("赵六","上海",34));
testDtoList.add(new TestDto("孙七","上海",18));
//按年龄排序
List<TestDto> reverseResList = testDtoList.stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());
List<TestDto> optionalList = Optional.ofNullable(testDtoList).orElse(null).stream().sorted(Comparator.comparing(TestDto::getAge).reversed()).collect(Collectors.toList());
//按单属性分组
Map<String, List<TestDto>> groupResList = testDt
本文详细介绍了如何使用Java Stream流对List集合进行操作,包括排序、分组、过滤元素以及如何进行数据收集和组装。通过实例代码和注释,读者将深入理解Stream流在集合处理中的应用。
订阅专栏 解锁全文

6115

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



