# 比较两个地图的键和值，并将差异存储到结果地图中。

go评论42阅读模式

Compare two maps by key and value and storing difference into resultant map

# 问题

I have two maps:

Map<Long, Long> map1 = new HashMap<>();
Map<Long, Long> map2 = new HashMap<>();

map1 map2
(1,1) (1,1)
(2,1) (2,1)
(3,1)

map1 map2
(1,1) (1,1)
(2,1) (2,1)
(3,1)
(4,1)

I know that some of you will mark it as duplicate, but didn't want to ask similar question in someone else thread.
I have two maps:

``````Map&lt;Long, Long&gt; map1 = new HashMap&lt;&gt;();
Map&lt;Long, Long) map2 = new HashMap&lt;&gt;();
``````

This two maps are different in size.
Maps will look like this:

``````map1        map2
(1,1)       (1,1)
(2,1)       (2,1)
(3,1)
``````

or:

``````map1        map2
(1,1)       (1,1)
(2,1)       (2,1)
(3,1)
(4,1)
``````

Now, I wan to create map3 that will contain only difference between map1 and map2, and that is (K,V)-->(3,1) ( (K,V)-->(3,1);(4,1) ). I will then get (K,V) values and use them.
In this thread similar question there is solution but not works for me. Passing to method `compareKeysAndValues(map1,map2)`, receiving: Required Map<Long, Long> provided Map<Long, Boolean)

Any Ideas?

# 答案1

``````显而易见的解决方案是在两个映射中遍历键，并将条目添加到新映射中，如果在另一个映射中不存在该键：

import java.util.HashMap;
import java.util.Map;

public class test {
public static void main(String[] args){
Map<Long, Long> map1 = new HashMap<>();
Map<Long, Long> map2 = new HashMap<>();
map1.put(1L, 1L);
map1.put(2L, 1L);
map1.put(5L, 1L);
map2.put(1L, 1L);
map2.put(2L, 1L);
map2.put(3L, 1L);
map2.put(4L, 1L);

Map<Long, Long> map3 = new HashMap<>();

for(Map.Entry<Long, Long> entry : map1.entrySet()){
if(!map2.containsKey(entry.getKey())){
map3.put(entry.getKey(), entry.getValue());
}
}

for(Map.Entry<Long, Long> entry : map2.entrySet()){
if(!map1.containsKey(entry.getKey())){
map3.put(entry.getKey(), entry.getValue());
}
}

for(Map.Entry<Long, Long> entry : map3.entrySet()){
System.out.println(entry.getKey() + ", " + entry.getValue());
}
}
}

3, 1
4, 1
5, 1
``````

An obvious solution is to traverse over the keys in both maps and add the entries to the new map if the key doesn't exist in the other map:

``````import java.util.HashMap;
import java.util.Map;
public class test {
public static void main(String[] args){
Map&lt;Long, Long&gt; map1 = new HashMap&lt;&gt;();
Map&lt;Long, Long&gt; map2 = new HashMap&lt;&gt;();
map1.put(1L, 1L);
map1.put(2L, 1L);
map1.put(5L, 1L);
map2.put(1L, 1L);
map2.put(2L, 1L);
map2.put(3L, 1L);
map2.put(4L, 1L);
Map&lt;Long, Long&gt; map3 = new HashMap&lt;&gt;();
for(Map.Entry&lt;Long, Long&gt; entry : map1.entrySet()){
if(!map2.containsKey(entry.getKey())){
map3.put(entry.getKey(), entry.getValue());
}
}
for(Map.Entry&lt;Long, Long&gt; entry : map2.entrySet()){
if(!map1.containsKey(entry.getKey())){
map3.put(entry.getKey(), entry.getValue());
}
}
for(Map.Entry&lt;Long, Long&gt; entry : map3.entrySet()){
System.out.println(entry.getKey() + &quot;, &quot; + entry.getValue());
}
}
}
``````

Output:

``````3, 1
4, 1
5, 1
``````

• 本文由 发表于 2020年7月29日 06:22:23
• 转载请务必保留本文链接：https://go.coder-hub.com/63143640.html
• hashmap
• java

go 48

go 38