JDK1.8Stream的一些用法

将Integer集合 转换为String集合

Set<String> userIDs = userByUserID.stream().map(TUser::getID).map(Functions.toStringFunction()).collect(Collectors.toSet());

这样可以取到对象里面的最大值

Optional<TUserReport> report = reported.stream().collect(Collectors.maxBy(Comparator.comparing(TUserReport::getPostTime)));
        Integer maxValue = songResultDemos.stream().map(SongResultDemo::getSongNum).max(Integer::compareTo).get();

这样可以一list集合里面的对象的一个字段 为key value 就是 整个对象并且是个list集合

Map<Integer, List<TUserReport>> reportedUserMapList = all.stream().collect(Collectors.groupingBy(TUserReport::getUserId));

将id 与 对象装入一个map集合 id对应的就是它的对象

Map<Integer, TAccount> allAccountIDMap = allAccountIDList.stream().collect(Collectors.toMap(TAccount::getId, Function.identity()));

获取到用户id的集合

Set<Integer> accountIDSet = topicByAccountAndTypeAndTime.stream().map(TTopicFeedOperateRecord::getAccountId).collect(Collectors.toSet());

排序的 一种方法

userReportDTOS.sort(Comparator.comparing(UserReportDTO::getPostTime).reversed());

双重排序

userReportVos.sort(Comparator.comparing(UserReportVo::getReportCount).thenComparing(UserReportVo::getLastReportTime).reversed());

分页

List<FeedVo> limitvos = productFeed.stream().skip((currentPage - 1) * 10).limit(10).collect(Collectors.toList());

过滤

List<LinkNameVo> collect = linkNameVos.stream().filter(linkNameVo -> auditorAndAuditeeList.stream().noneMatch(e -> e.equals(linkNameVo.getId()))).collect(Collectors.toList());

去掉对象集合里面有相同字段的对象

List<QualityTeamVo> collect1 = qualityTeamVos.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(QualityTeamVo::getDepartmentName))), ArrayList::new));

过滤出第一个 并且有两个条件

AliGreenVo aliGreenVo = aliGreenVoList.stream().filter(aliGreen -> "review".equals(aliGreen.getSuggestion()) ||
                    "block".equals(aliGreen.getSuggestion())).findFirst()
                    .orElse(new AliGreenVo(null, null, "normal", "pass", null));
                

将集合里面的某个字段 相同的去除

List<TBigvList> collect = accountNickname1.stream()
                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getAccountNickname()))), ArrayList::new));					

获取对象里所有 level 等于 3的 对象

   List<ShowProjectVo> third = result.stream().filter(s -> Integer.valueOf(3).equals(s.getLevel())).collect(Collectors.toList());    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值