stream流分组 过滤 排序 list转换map 便利开发

1.使用stream流进行分组
Map<String, List<String>> collect = list.stream().collect(Collectors.groupingBy(pojo::name));
2.使用stream流进行过滤
List<String> collect = list.stream().filter(a -> a != null && "aaa".equals(a)).collect(Collectors.toList());
3.list根据属性转map
3.1.1简单对象属性转map
//简单对象属性转map
Map<Long, String> collect = list.stream().collect(Collectors.toMap(User::getId, User::getIcon));
3.1.2指定对象中的某个属性作为键值
//给出key重复时,使用哪个key作为主键,以下代码中的(key1, key2) -> key2)代表key1和key2键重复时返回key2做主键
Map<Long, User> map = list.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
4.对获得的list其中对象的某个属性进行排序
降序
//使用stream中的sort进行排序 根据其中的属性进行排序
List<User> collect1 = list.stream().sorted(Comparator.comparing(User::getCreateTime).reversed()).collect(Collectors.toList());
升序
//正常升序
list.stream().sorted(Comparator.comparing(User::getAge)) 
不使用stream排序
// 正序
list.sort(Comparator.comparing(User::getAge));
  // 倒序
list.sort(Comparator.comparing(User::getAge).reversed());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值