首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从多个贴图创建嵌套贴图

从多个贴图创建嵌套贴图
EN

Stack Overflow用户
提问于 2020-07-14 06:53:43
回答 1查看 80关注 0票数 0

我有一些散列映射,如下所示。我想把这些地图合并成一个嵌套地图。

代码语言:javascript
复制
public class MyClass {
    public static void main(String[] args) {
        HashMap<String, Capacity> capacityMap1 = new HashMap<>();
        capacityMap1.put("BMW", new Capacity(10.0, 0.0));
        capacityMap1.put("Audi", new Capacity(20.0, 10.0));
        capacityMap1.put("Toyota", new Capacity(50.0, 0.0));


        HashMap<String, Capacity> capacityMap2 = new HashMap<>();
        capacityMap1.put("BMW", new Capacity(0.0, 10.0));
        capacityMap1.put("Audi", new Capacity(80.0, 0.0));
        capacityMap1.put("Toyota", new Capacity(90.0, 0.0));

        HashMap<String, Capacity> capacityMap3 = new HashMap<>();
        capacityMap1.put("BMW", new Capacity(30.0, 0.0));

        HashMap<String, Capacity> capacityMap4 = new HashMap<>();
        capacityMap1.put("Audi", new Capacity(80.0, 0.0));
        capacityMap1.put("Toyota", new Capacity(90.0, 10.0));
    }

    enum UsageConsumed {
        Inbound,
        OutBound,
    }

    static class Capacity {
        double inBoundCapacity;
        double outBoundcapacit;

        public Capacity(double inBoundCapacity, double outBoundcapacit) {
            this.inBoundCapacity = inBoundCapacity;
            this.outBoundcapacit = outBoundcapacit;
        }
    }

}

我想创建一个嵌套的映射列表HashMap>,其中的键将是上面映射的键。如果密钥不存在,那么我们将使用容量的值创建一个新的hashmap。如果键已经存在,那么我们希望将该值添加到现有的键中。我如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2020-07-14 08:04:15

我认为这应该可以做到:

代码语言:javascript
复制
public static Map<String, HashMap<UsageConsumed, Double>> buildNestedMap(HashMap<String, Capacity>... maps) {

    Map<String, HashMap<UsageConsumed, Double>> result = Stream.of(maps)
            // get a stream of all the map entries
            .flatMap(m -> m.entrySet().stream())
            .collect(Collectors.toMap(
                    Map.Entry::getKey,
                    // create the value for a single entry
                    entry -> {
                        HashMap<UsageConsumed, Double> usageConsumedMap = new HashMap<>();
                        usageConsumedMap.put(UsageConsumed.Inbound, entry.getValue().inBoundCapacity);
                        usageConsumedMap.put(UsageConsumed.OutBound, entry.getValue().outBoundcapacit);
                        return usageConsumedMap;
                    },
                    // merge the values between entries with the same key
                    (a, b) -> {
                        a.merge(UsageConsumed.Inbound, b.get(UsageConsumed.Inbound), Double::sum);
                        a.merge(UsageConsumed.OutBound, b.get(UsageConsumed.OutBound), Double::sum);
                        return a;
                    }
                    ));

    return result;
}

使用以下代码进行测试:

代码语言:javascript
复制
public static void main(String[] args) {
    HashMap<String, Capacity> capacityMap1 = new HashMap<>();
    capacityMap1.put("BMW", new Capacity(10.0, 0.0));
    capacityMap1.put("Audi", new Capacity(20.0, 10.0));
    capacityMap1.put("Toyota", new Capacity(50.0, 0.0));
    
    HashMap<String, Capacity> capacityMap2 = new HashMap<>();
    capacityMap2.put("BMW", new Capacity(0.0, 10.0));
    capacityMap2.put("Audi", new Capacity(80.0, 0.0));
    capacityMap2.put("Toyota", new Capacity(90.0, 0.0));

    HashMap<String, Capacity> capacityMap3 = new HashMap<>();
    capacityMap3.put("BMW", new Capacity(30.0, 0.0));

    HashMap<String, Capacity> capacityMap4 = new HashMap<>();
    capacityMap4.put("Audi", new Capacity(80.0, 0.0));
    capacityMap4.put("Toyota", new Capacity(90.0, 10.0));

    Map<String, HashMap<UsageConsumed, Double>> result = buildNestedMap(capacityMap1, capacityMap2, capacityMap3, capacityMap4);

    System.out.println(result);
}

输出为:

代码语言:javascript
复制
{Toyota={Inbound=230.0, OutBound=10.0}, Audi={Inbound=180.0, OutBound=10.0}, BMW={Inbound=40.0, OutBound=10.0}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62885530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档