Java中Stream流对List集合排序、分组、过滤、收集组装、聚合处理等

本文详细介绍了如何使用Java Stream流对List集合进行操作,包括排序、分组、过滤元素以及如何进行数据收集和组装。通过实例代码和注释,读者将深入理解Stream流在集合处理中的应用。

代码和注释如下:


        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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值