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

huangapple 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)

现在,我想创建map3,它将只包含map1和map2之间的差异,即(K,V)-->(3,1) ( (K,V)-->(3,1);(4,1) )。然后,我将获取(K,V)的值并使用它们。
在这个类似的问题中有解决方案,但对我不起作用。传递给方法compareKeysAndValues(map1,map2),收到:提供的Map<Long, Long>要求的Map<Long, Boolean>。

有什么想法?
1: https://stackoverflow.com/questions/21559811/how-to-compare-two-maps-by-both-key-and-value-and-storing-difference-map-in-resu

英文:

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

得分: 0

以下是您要翻译的代码部分:

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

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

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

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定