常用lambda表达式汇总

lambda表达式:
1、获取对象list其中一个属性放进List

List<String> Ids = newList.stream().map(UserInfoDto::getId).collect(Collectors.toList());

2、获取对象list,其中一个唯一属性放进Map

Map<String, UserInfoDto> userMap = userList.stream().collect(Collectors.toMap(UserInfoDto::getId, userInfoDto -> userInfoDto));

3、取2个List的交集

List<UserInfoDto> newList = restList.stream().filter(item -> userList.stream().map(UserInfoDto::getId).collect(Collectors.toList()).contains(item.getId())).collect(Collectors.toList());

4、过滤对象list其中一个属性符合条件的属性放进List

List<String> ids = newList.stream()..filter(c -> !c.getFirstName.equals("吴")).collect(Collectors.toList());

result = result.stream().filter(a -> a.getStatue().equals("01) && a.getAge() >= 10).collect(Collectors.toList());

5、排序场景对象List按照一个对象其中一个属性排序

result.sort((h1, h2) -> h1.getTimePoint().compareTo(h2.getTimePoint()));

6、根据对象List其中某一个属性分组汇总成Map<String,List>

Map<String, List<TestDto>> map = result.stream().collect(Collectors.groupingBy(TestDto::getTimePoint));
Map<String, List<String>> newMap = new HashMap<>();
for (Map.Entry<String, List<TestDto>> entry : map.entrySet()) {
    newMap.put(entry.getKey(), entry.getValue().stream().map(TestDto::getType).collect(Collectors.toList()));
}

7、根据对象List其中某一个属性分组汇总成Map<String,List>

Map<String, List<TestDto>> map = result.stream().collect(Collectors.groupingBy(TestDto::getTimePoint));

8、Map<String,List> 根据key排序

newMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> resultMap.put(x.getKey(), x.getValue()));
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涯一涯二涯三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值