Java8 Steam 随笔记录

  1. 两个List集合过滤并根据比较找出差集,并将差集归类为List
    List<BetLimit> changeBetLimitList = list.stream().filter(item -> item.getSingleOrderMinStake() < oldData.getSingleOrderMinStake()).collect(Collectors.toList())
  2. List转Map
    Map<Integer, BetLimit> oldBetLimitMap = oldBetLimitList.stream().collect(Collectors.toMap(BetLimit::getId, o -> o))

     

  3. List提取属性放入List
     

    List<Integer> betItemIdList = list.stream().map(Order::getBetItemId).distinct().collect(Collectors.toList());
  4. List通过join,转字符串
    String inIds = accountIdList.stream().map(i -> i.toString()).collect(Collectors.joining(","));
  5. List<T> group by Map<Integer,List<T>>
    Map<BigDecimal, List<Item>> groupByPriceMap= items.stream().collect(Collectors.groupingBy(Item::getPrice));
  6. List<T> 多属性分组group by Map<Integer,Map<Integer,List<T>>
    Map<Integer,Map<String,List<DocLotDetail>>> amp                            = docLotDetailLists.parallelStream().collect(Collectors.groupingBy(DocLotDetail::getGroupOrder,Collectors.groupingBy(DocLotDetail::getBaseValueName)));
  7. List<T> 多属性唯一分组Map<Integer,Map<Integer,T>>
    Map<Integer, Map<String, PercentDistribution>> percentDistributionLotteryMap =
            percentDistributionList.stream().collect
                    (Collectors.groupingBy(PercentDistribution::getAccountId, Collectors.toMap
                            (PercentDistribution::getLotteryCode, o -> o)));

8.BigDecimal求和

int totalBetStake = list.stream().map(Order::getBetStake).reduce(BigDecimal.ZERO,
        BigDecimal::add)
        .intValue();

9.groupBy转换Map类型

Map<Date, List<Order>> dateOrderMap = orderList.stream().collect(Collectors.groupingBy(Order::getReportDate,
        LinkedHashMap::new, Collectors.toList()));

10.List中对象多个属性求和,并返原对象

  A a = list.stream() .reduce((x , y) -> new A( (x.getPrincipal() + y.getPrincipal()), (x.getFee() + y.getFee()) ) ).orElse( new A(0, 0) );

11.groupby 按某个属性分组,并收集为一个属性集合

Map<Integer, List<Integer>> collect = list.stream().collect(Collectors.groupingBy(record -> record
        .getInt("accountId"), Collectors.mapping(record -> record.getInt("permissionId"), Collectors.toList())

转载于:https://my.oschina.net/u/127459/blog/3008620

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值