【问题标题】:Create a nested map from multiple map从多个地图创建嵌套地图
【发布时间】:2020-07-13 22:53:43
【问题描述】:

我有几个哈希映射如下。我想将这些地图合并到一个嵌套地图中。

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> 其中的键将是上述映射的键。如果键不存在,那么我们创建一个具有容量值的新哈希图。如果密钥已经存在,那么我们希望将值添加到现有密钥中。我怎样才能做到这一点?

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

    我认为应该这样做:

    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;
    }
    

    用这段代码测试过:

    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);
    }
    

    输出是:

    {Toyota={Inbound=230.0, OutBound=10.0}, Audi={Inbound=180.0, OutBound=10.0}, BMW={Inbound=40.0, OutBound=10.0}}
    

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 1970-01-01
      • 2015-09-27
      • 2014-10-18
      • 1970-01-01
      • 2021-06-16
      • 2011-01-27
      • 1970-01-01
      • 2014-08-14
      相关资源
      最近更新 更多