Java stream 那些事

1.求和

LinkedHashMap<String, Double> countMap = hourList.stream().collect(Collectors.groupingBy(o ->o.getCheckDate() + "-" + o.getCheckHour(), LinkedHashMap::new,
        Collectors.summingDouble(s->{
            EnergyTypeEnum type = EnergyTypeEnum.getByValue(s.getEnergyType());
            return  EnergyTypeIdList.CalcConsume(type,s.getAmount());
List<EnergyConsumeHour>  hourList1 = countMap.keySet().stream().map(key -> {
    String[] temp = key.split("-");
    String checkHour = temp[1];
    String checkDate = temp[0];
    EnergyConsumeHour record = new  EnergyConsumeHour();
    record.setCheckDate(checkDate);
    record.setCheckHour(Integer.valueOf(checkHour));
    record.setAmount(countMap.get(key));
    return record;
}).collect(Collectors.toList());2

2.求平均

 
Map<Integer, Double> lisXMap3 = list.stream().collect(Collectors.groupingBy(AnalyseVo::getCheckHour,
        Collectors.averagingDouble(AnalyseVo::getAmount)));

3.Map

 Map<Integer, Double> lisXMap2 = list.stream().filter(one -> one.getDateDesc().equals(lastDay))
                .collect(Collectors.toMap(AnalyseVo::getCheckHour, AnalyseVo::getAmount));







 return list.stream()
                    .collect(Collectors.groupingBy(SysProjectGroupVo::getGroupLevel,
                            Collectors.mapping(SysProjectGroupVo::getOpenId, Collectors.joining(","))));

4. reduce 求和

BigDecimal reduce = children.stream().map(line ->
				Optional.ofNullable(line.getInboundQuantity()).orElse(BigDecimal.ZERO))
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sum = lineList.stream().map(DtwmsPoInventoryLineBo::getDiscountTaxAmount).reduce(BigDecimal.ZERO, (acc, item)->{
            return acc.add(item);
        });
BigDecimal totalAmount = list.stream().map(x -> x.getPrice().multiply(x.getSaleQty())).reduce(BigDecimal.ZERO, BigDecimal::add);
//                        BigDecimal prodRate = list.stream().map(x -> x.getProdRate()).reduce(BigDecimal.ZERO, BigDecimal::add);

5.peek

 pageData.getRecords().stream().peek(data -> {
                    data.setBmsOrderStatus(getBMSOrderStatus(data, finalStatusModeMap,null,null));
                }).collect(Collectors.toList());
 boolean present = roleList.stream().filter(a -> a.getRoleId().equals(req.getRoleId())).findAny().isPresent();


boolean present = roleList.stream().anyMatch(a -> a.getRoleId().equals(req.getRoleId()));


说明:
anyMatch():匹配到任何一个元素和指定的元素相等,返回 true
allMatch():匹配到全部元素和指定的元素相等,返回 true
noneMatch():与 allMatch() 效果相反
Map<String, List<Working>> map =
                workings.stream().collect(Collectors.toMap(Working::getInvoicePage,
                        e -> {
                            ArrayList<Working> list = new ArrayList<>();
                            list.add(e);
                            return list;
                        },
                        (oldList, newList) -> {
                            oldList.addAll(newList);
                            return oldList;
                        }));
		// 或者使用groupBy
		// 存为List
        Map<String, List<BusinessSceneDetail>> collect0 =
                sceneDetailMap.values().stream().collect(
                        Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode));
        // 存为set
        Map<String, Set<BusinessSceneDetail>> collect =
                sceneDetailMap.values().stream().collect(
                        Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.toSet()));
        // 多层map
        Map<String, Map<String, Set<BusinessSceneDetail>>> collect1 = sceneDetailMap.values().stream().collect(
                Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode, Collectors.groupingBy
                        (BusinessSceneDetail::getBusinessSceneCode, Collectors.toSet())));
        // 对象子属性 map
        Map<String, List<String>> collect2 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
                (BusinessSceneDetail::getDataSourceCode, Collectors.mapping(BusinessSceneDetail::getRuleContent,
                        Collectors.toList())));
        // 对象按照规则获取一个
        Map<String, BusinessSceneDetail> collect3 =
                sceneDetailMap.values().stream().collect(Collectors.groupingBy
                        (
                                BusinessSceneDetail::getDataSourceCode,
                                Collectors.collectingAndThen(
                                        Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
                                        Optional::get
                                )
                        ));

        // 对象按照规则获取一个里面的值 p肯定存在
        Map<String, String> collect4 = sceneDetailMap.values().stream().collect(Collectors.groupingBy
                (
                        BusinessSceneDetail::getDataSourceCode,
                        Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)),
                                p -> p.get().getRuleContent()
                        )
                ));
                
        // map的value进行排序
           Map<String, List<SysDictionary>> map = sysDictionaryList.stream().collect(
                Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>((Comparator<Object>) (o1, o2) -> 0)),
                                ArrayList::new
                        )
                )
        );
        // map的key进行排序
 		Map<String, List<SysDictionary>> map1 = sysDictionaryList.stream().collect(
                Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        TreeMap::new,
                        Collectors.toList()
                )
        );

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值