(0)Java 8 stream-手动编写报表明细实现sql并发

直接上代码:

 @GetMapping("/report")
    public String report() {
        List<String> citys = Arrays.asList("宿城", "宿豫", "泗洪", "泗阳", "沭阳");
        Stream<String> cityStream = citys.parallelStream();
        List<HashMap<String, List<HashMap<String, String>>>> collect = cityStream.map(s -> {
            try {
                return getCalResult(s);
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
//        System.out.println(collect);
        Gson gson = new Gson();
        String s = gson.toJson(collect);
        return s;
    }

    private HashMap<String, List<HashMap<String, String>>> getCalResult(String city) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "msaj").thenApply(s -> mxajService.getMszj() + "");
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "xsaj").thenApply(s -> mxajService.getXszj() + "");
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "xzaj").thenApply(s -> mxajService.getXzzj() + "");

        CompletableFuture<List<HashMap<String, String>>> anyOf = CompletableFuture.allOf(future1, future2, future3).thenApply(new Function<Void, List<HashMap<String, String>>>() {

            @Override
            public List<HashMap<String, String>> apply(Void aVoid) {
                try {
                    String r1 = future1.get();
                    String r2 = future2.get();
                    String r3 = future3.get();
                    HashMap<String, String> map1 = new HashMap<String, String>();
                    map1.put("msaj", r1);
                    HashMap<String, String> map2 = new HashMap<String, String>();
                    map2.put("xsaj", r2);
                    HashMap<String, String> map3 = new HashMap<String, String>();
                    map3.put("xzaj", r3);
                    List<HashMap<String, String>> result = Arrays.asList(map1, map2, map3);
                    return result;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }).exceptionally(new Function<Throwable, List<HashMap<String, String>>>() {
            @Override
            public List<HashMap<String, String>> apply(Throwable throwable) {
                return null;
            }
        });

        HashMap<String, List<HashMap<String, String>>> map = new HashMap<>();
        map.put(city, anyOf.get());
        return map;
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值