简体   繁体   English

合并Hashmaps保留值JAVA

[英]Merge Hashmaps retaining values JAVA

Say I have two hashmaps: 假设我有两个哈希图:

  1. {dog=1, cat=1, sport=3, golf=4} {dog = 1,cat = 1,sport = 3,golf = 4}
  2. {dog=5, person=1, animal=4} {dog = 5,person = 1,animal = 4}

I want to merge them retaining the values, so that the values add together in the output 我想合并它们保留值,以便值在输出中相加

Output: 输出:

{ dog=6 , person=1, animal=4, cat=1, sport=3, golf=4} { dog = 6 ,person = 1,animal = 4,cat = 1,sport = 3,golf = 4}

Notice how "dog" has a value of 6 (5+1) 注意“dog”的值是6 (5 + 1)

Using Hashmap.putAll(), the value of dog becomes 5, any ideas how I can merge these retaining the values? 使用Hashmap.putAll(),dog的值变为5,任何想法如何合并这些保留值?

Many thanks, Philip 非常感谢,菲利普

Google's open source Guava Library has a class called Multiset which entirely abstracts out the need for a Map<T,Integer> for counting instances of T . Google的开源Guava Library有一个名为Multiset的类,它完全抽象出需要Map<T,Integer>来计算T实例。

You'll find that using Multiset<String> instead of using a Map<String,Integer> will result in less code which is less error prone to do the same thing. 您会发现使用Multiset<String>而不是使用Map<String,Integer>将导致更少的代码,这些代码不易出错。

Using Multiset , to merge the two you would just say: 使用Multiset ,将两者合并,你只会说:

multiset1.addAll(multiset2);
HashMap merged = new HashMap<String, Integer>();

for (String x : map1.getKeySet()) {
   Integer y = map2.get(x);
   if (y == null) {
      merged.put(x, map1.get(x));
   } else {
      merged.put(x, map1.get(x)+y);
   }
}

for (String x : map2.getKeySet()) {
   if (merged.get(x) == null) {
      merged.put(x, map2.get(x));
   }
} 

Just threw that together - not saying it's the best way. 把它扔在一起 - 不是说这是最好的方式。

取较大的一个,然后沿着较小的一个循环,添加不存在的条目,并调整现有条目的值。

If you put a duplicate key, value into a map in java it will replace whatever value was previously there. 如果你把一个重复的键,值放入java中的一个映射中,它将替换之前的任何值。

You could check to see if the value exists so 您可以检查该值是否存在

Map outputMap = new HashMap<String, Integer>()
for(Map.Entry<String, Integer> entry : map1.entrySet()){
   if(map2.contains(entry.getKey()){
     outputMap.put(entry.getKey(), entry.getValue() + map2.get(entry.getKey());
     map2.remove(entry.getKey());  
   }
   else
     outputMap.put(entry.getKey(), entry.getValue()
}
outputMap.addAll(map2); 

This would be the simplest non destructive way to do it although it might not be as efficient as two loops since both keysets are inserted into a 3rd. 这将是最简单的非破坏性方法,尽管它可能不如两个循环有效,因为两个键集都插入到第三个。

HashSet<String> allKeys = new HashSet<String>();
HashMap<String, Integer> resultMap = new HashMap<String, Integer>();
allKeys.addAll(map1.keySet());
allKeys.addAll(map2.keySet());
for (String k : allKeys) {
  int i1 = map1.containsKey(k) ? map1.get(k) : 0;
  int i2 = map2.containsKey(k) ? map2.get(k) : 0;
  resultMap.put(k, i1 + i2);
}

To shorten the loops you can. 要缩短循环,你可以。

Map<String, Integer> merged = new HashMap<String, Integer>(map1); 
for (Map.Entry<String, Integer> entry : map2.entrySet()) {
   Integer y = merged.get(entry.getKey()); 
   merged.put(entry.getKey(), entry.getValue() + (y == null ? 0 : y));
} 

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

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