Java8-实践

Java8-实践

  • Map的双重循环

        //对map的entry对象来做stream操作,使用两次forEach
        Map<String, Long> map = new HashMap<>();
        crowdMap.entrySet().stream()
                .map(Map.Entry::getValue)
                .forEach(x -> x.entrySet().forEach(y -> {
                    if (map.containsKey(y.getKey()))
                        map.put(y.getKey(), map.get(y.getKey()) + y.getValue());
                    else map.put(y.getKey(), y.getValue());
                }));
    
        //对map的entry对象来做stream操作,使用flatMap将stream合并
        Map<String, Long> map = new HashMap<>();
        crowdMap.entrySet().stream()
                .map(Map.Entry::getValue)
                .flatMap(x -> x.entrySet().stream())
                .forEach(y -> {
                    if (map.containsKey(y.getKey()))
                        map.put(y.getKey(), map.get(y.getKey()) + y.getValue());
                    else map.put(y.getKey(), y.getValue());
                });
    
        //使用map本身的foreach语句            
        Map<String, Long> map = new HashMap<>();
        crowdMap.forEach((key, value) -> value.forEach((x, y) -> {
            if (map.containsKey(x))
                map.put(x, map.get(x) + y);
            map.put(x, y);
        }));
  • List的多次分组

        //使用groupingBy将ApproveRuleDetail对象分别按照item和detail分组,并计次
        Map<String, Map<String, Long>> detailMap = approveRuleDetailList.stream()
                .collect(Collectors
                        .groupingBy(ApproveRuleDetail::getItem, Collectors.
                                groupingBy(ApproveRuleDetail::getDetail, Collectors.counting())));
  • List按照自定义条件分组

        //使用自定义的Function函数,将年龄按照每10岁分组
        Function<Integer, Integer> ageGroup = x -> x / 10;
        Map<Integer, List<StatisticsPipeline>> ageMap = statisticsPipelineList
                .stream()
                .collect(Collectors.groupingBy(y -> ageGroup.apply(y.getAge())));
        //将年龄按不同方式分组
        Function<Integer, Integer> ageCredit = x -> {
            if (x <= 18)
                return 18;
            else if (x >= 40)
                return 40;
            else return x;
        };
    
        //将StatisticsPipeline转化为suggestion
        ToDoubleFunction<StatisticsPipeline> mapper = StatisticsPipeline::getSuggestion;
    
        //将人群按照不同年龄分组,并计算每个年龄段的suggestion的平均值
        Map<Integer, Double> ageCreditMap = statisticsPipelineList
                .stream()
                .collect(Collectors.groupingBy(y -> ageCredit.apply(y.getAge()), Collectors.averagingDouble(mapper)));
  • 多个数据求集合

        //合并数据
        private BiFunction<Integer[], ApprovePipeline, Integer[]> accumulator = (x, y) -> new Integer[]{
                x[0] + y.getAuth(), x[1] + y.getAntiFraud(), x[2] + y.getCreditRule(), x[3] + y.getModelReject(), x[4] + y.getSuggestion()
        };
    
        //合并集合
        private BinaryOperator<Integer[]> combiner = (x, y) -> new Integer[]{x[0] + y[0], x[1] + y[1], x[2] + y[2], x[3] + y[3], x[4] + y[4]};
    
        //将ApprovePipeline对象的不同数据相加
        Integer[] detail = approvePipelineList.stream().reduce(new Integer[]{0, 0, 0, 0, 0}, accumulator, combiner);
  • 多个数据求集合-多重合并

        private BiFunction<Integer[], ApprovePipeline, Integer[]> accumulator = (x, y) -> new Integer[]{
            x[0] += y.getAuth(), x[1] += y.getAntiFraud(), x[2] += y.getCreditRule(), x[3] += y.getModelReject(), x[4] += y.getSuggestion()
        };
        //合并数据
        BiFunction<Integer[], Map.Entry<String, List<ApprovePipeline>>, Integer[]> newAccumulator = (x, y) -> {
            List<ApprovePipeline> pipelineList = y.getValue();
            Integer[] data = pipelineList.stream().reduce(new Integer[]{0, 0, 0, 0, 0}, accumulator, combiner);
            return new Integer[]{
                    x[0] += data[0], x[1] += data[1], x[2] += data[2], x[3] += data[3], x[4] += data[4]
            };
        };
        //最终reduce
        Integer[] total = channelMap.entrySet().stream().reduce(new Integer[]{0, 0, 0, 0, 0}, newAccumulator, combiner);
  • map最大多项和

    Long hourC3 = hourMap.entrySet().stream().mapToLong(Map.Entry::getValue).sorted().limit(3).sum();

    项目地址https://github.com/MyHerux/java-java8-learning

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值