码迷,mamicode.com
首页 > 其他好文 > 详细

lambda concurrent List<Map> to Map

时间:2019-12-22 18:28:58      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:join   cat   turn   article   www   sync   print   exce   sleep   

Object c = Stream.of(
        CompletableFuture.supplyAsync(() -> {
            Map m = new HashMap();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("a order");
            m.put("a1", "a1");
            m.put("a2", "a2");
            return m;
        }),
        CompletableFuture.supplyAsync(() -> {
            Map m = new HashMap();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            m.put("b1", "b1");
            m.put("b2", "b2");
            System.out.println("b order ");
            return m;
        }))
        .parallel()
        .flatMap(v -> ((Set<Map.Entry>) Optional.of(v.join()).orElse(new HashMap()).entrySet()).stream())
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> k1 == null ? k2 : k1));
System.out.println(c);

lambda concurrent List<Map> to Map

标签:join   cat   turn   article   www   sync   print   exce   sleep   

原文地址:https://www.cnblogs.com/1994july/p/12080427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!