简体   繁体   English

如何将java中的多个Hashmap放入一个新的hashmap中?

[英]How can put multiple Hashmap in java into a new hashmap?

I have eight HashMaps in Java as follows:我在 Java 中有八个 HashMap,如下所示:

     HashMap<String, Double> map1= new HashMap<String, Double>();
        HashMap<String, Double> map2= new HashMap<String, Double>();
        HashMap<String, Double> map3= new HashMap<String, Double>();
        HashMap<String, Double> map4= new HashMap<String, Double>();
        HashMap<String, Double> map5= new HashMap<String, Double>();
        HashMap<String, Double> map6= new HashMap<String, Double>();
        HashMap<String, Double> map7= new HashMap<String, Double>();
        HashMap<String, Double> map8= new HashMap<String, Double>();

I want to merge all in a new HashMap lets say NEW_MAP.我想将所有内容合并到一个新的 HashMap 中,比如说 NEW_MAP。 How can I do this?我怎样才能做到这一点?

I am not sure if I am following your question correctly,there is a putAll() method in Map interface, you can merge them all to new HashMap by this method我不确定我是否正确地回答了您的问题,Map 接口中有一个 putAll() 方法,您可以通过此方法将它们全部合并到新的 HashMap

eg :例如:

Map<K,V> nhm = new HashMap<>();

nhm.putAll(map1);
...........
...........
nhm.putAll(map8);

Then decide what to do with duplicate keys and use the third parameter of Collectors#tomap: (Collectors.toMap(keyMapper, valueMapper, mergeFunction))然后决定如何处理重复的键并使用 Collectors#tomap 的第三个参数:(Collectors.toMap(keyMapper, valueMapper, mergeFunction))

Stream.of(map1, map2,map3,map4, map4,map5,map6,map7,map8)
      .flatMap(m -> m.entrySet().stream()) 
      .collect(Collectors.toMap(
                Entry::getKey, Entry::getValue, (oldValue, newValue) -> newValue
              )
      );

The above for example will keep the last seen value if duplicates are ditected.例如,如果检测到重复项,则上面的示例将保留最后看到的值。 You can change it to whatever you want according to your requirements:您可以根据您的要求将其更改为您想要的任何内容:

keep the first value seen: (oldValue, newValue) -> oldValue保持看到的第一个值:(oldValue, newValue) -> oldValue

merge the two, eg by adding them: (oldValue, newValue) -> old + new合并两者,例如通过添加它们:(oldValue, newValue) -> old + new

merge the two, eg by multiplying them: (oldValue, newValue) -> old * new合并两者,例如通过将它们相乘:(oldValue, newValue) -> old * new

remove value: (oldValue, newValue) -> null删除值:(oldValue, newValue) -> null

.... ....

You can combine usage of putAll from RIPAN and Stream.of from Eritrean for simple putAll:您可以结合使用来自RIPANputAll和来自厄立特里亚的Stream.of来实现简单的 putAll:

HashMap<String, Double> NEW_MAP= new HashMap<String, Double>();
Stream.of(map1, map2,map3,map4, map4,map5,map6,map7,map8)
.forEach(NEW_MAP::putAll);

But if you want some custom logic during merging duplicate keys, you can do as below (with choosing min value for example):但是,如果您在合并重复键期间需要一些自定义逻辑,您可以执行以下操作(例如选择最小值):

HashMap<String, Double> NEW_MAP= new HashMap<String, Double>();
Stream.of(map1, map2,map3,map4, map4,map5,map6,map7,map8)
        .flatMap(m -> m.entrySet().stream())
        .forEach(e -> NEW_MAP.merge(e.getKey(), e.getValue(),
                (v1, v2) -> Math.min(v1, v2)
        ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM