Java 只要存在键匹配,就添加两个映射的值

Java 只要存在键匹配,就添加两个映射的值,java,map,add,Java,Map,Add,假设我有一张像这样的地图 {one=1; two=1; three=1} 还有一张像这样的地图 {one=4; two=4; three=4} 我知道putAll()将添加唯一键并替换现有键。 是否可以添加两个映射,这将产生一个类似于在存在现有关键字时添加值的结果 {one=5; two=5; three=5} 创建一个,然后在map1中为x,如果map2x,使用键x将两个值添加到新地图中,请尝试此操作 Map<String, Integer> map

假设我有一张像这样的地图

{one=1; two=1; three=1}
还有一张像这样的地图

{one=4; two=4; three=4}
我知道putAll()将添加唯一键并替换现有键。 是否可以添加两个映射,这将产生一个类似于在存在现有关键字时添加值的结果

{one=5; two=5; three=5}
创建一个,然后在
map1
中为
x
,如果
map2
x
,使用键
x
将两个值添加到新地图中,请尝试此操作

             Map<String, Integer> map = new HashMap<String, Integer>();
    Map<String, Integer> map1 = new HashMap<String, Integer>();
             Map<String, Integer> map2 = new HashMap<String, Integer>();

    map.put("one", 1);
    map.put("two", 1);
    map.put("three", 1);

    map1.put("one", 4);
    map1.put("two", 4);
    map1.put("three", 4);


    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(map1.get(entry.getKey())+entry.getValue());
                        map2.put(entry.getKey(),map1.get(entry.getKey())+entry.getValue());
    }
Map Map=newhashmap();
Map map1=新的HashMap();
Map map2=新的HashMap();
地图.放("一",一);;
地图.放(二),一;;
地图放置(“三”,1);
地图1.放置(“一”,4);
地图1.put(“两个”,4);
地图1.放置(“三”,4);
对于(Map.Entry:Map.entrySet()){
System.out.println(map1.get(entry.getKey())+entry.getValue());
map2.put(entry.getKey()、map1.get(entry.getKey())+entry.getValue());
}
扩展并重写putAll方法

public class MyMap extends java.util.HashMap{
 @Override
 public void putAll(java.util.Map mapToAdd){
      java.util.Iterator iterKeys = keySet().iterator();
      while(iterKeys.hasNext()){
           String currentKey = (String)iterKeys.next();
           if(mapToAdd.containsKey(currentKey)){
                mapToAdd.put(currentKey, new Integer(Integer.parseInt(get(currentKey).toString()) + Integer.parseInt(mapToAdd.get(currentKey).toString())));
           }else{
                mapToAdd.put(currentKey, get(currentKey));
           }
      }
      super.putAll(mapToAdd);
 }
 public static void main(String args[]){
     MyMap m1 = new MyMap();
     m1.put("One", new Integer(1));
     m1.put("Two", new Integer(2));
     m1.put("Three", new Integer(3));
     MyMap m2 = new MyMap();
     m2.put("One", new Integer(4));
     m2.put("Two", new Integer(5));
     m2.put("Three", new Integer(6));
     m1.putAll(m2);
     System.out.println(m1);
 }
}


现在,创建MyMap的对象,而不是HashMap。创建了一个小提琴

试试这个方法。使用泛型密钥类型。当我们添加它们时,值类型保持为
Integer

public <K> void putAndAdd(Map<K, Integer> to,
        Map<? extends K, ? extends Integer> from) {
    for (Iterator<? extends Map.Entry<? extends K, ? extends Integer>> i = from
            .entrySet().iterator(); i.hasNext();) {
        Map.Entry<? extends K, ? extends Integer> e = i.next();
        if (to.get(e.getKey()) != null) {
            to.put(e.getKey(), to.get(e.getKey()) + e.getValue());
        } else {
            to.put(e.getKey(), e.getValue());
        }
    }
}
public void putAndAdd(映射到、,

MapYes,大约5-6行代码。您是否尝试过任何操作?请参见此处,循环浏览一个映射,获取第二个映射中匹配键的值,将两个值相加,然后将其设置回第二个映射。尝试一下,然后告诉我们结果。您想知道是否有提供的方法(如
putAll
)这样就可以了?或者你是想让我们为你写代码?只是想让问题简单一点。我正在尝试一些并行编程,其中我会有4个大小约为2/3GB的映射。因此,我认为循环它们会对我的处理器造成负担,并想知道是否有任何可用的方法。谢谢。但我认为我注意到
HashMap
中的实现可能正在执行其他操作,这些操作将被此重写方法跳过。例如,我刚刚发现在某些情况下,
putAll
中调用了一个
resize
方法。@nishantshresh使用super.putAll更新了我的答案,以便我们的实现不会跳过actual HashMap实现。通过添加,您将否定您所做的所有工作。而且您不应该从map继承,因为MyMap在行为上不再是一个map。请创建一个简单的实用程序方法。@Esailija我正在更新输入映射,然后最后将输入映射元素放入当前映射中。请看这里的小提琴-