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
     java

    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求和app

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

9.groupBy轉換Map類型code

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) );ip

11.groupby 按某個屬性分組,並收集爲一個屬性集合ci

Map<Integer, List<Integer>> collect = list.stream().collect(Collectors.groupingBy(record -> record
        .getInt("accountId"), Collectors.mapping(record -> record.getInt("permissionId"), Collectors.toList())
相關文章
相關標籤/搜索