英文:
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<Long, Long> map1 = new HashMap<>();
Map<Long, Long) map2 = new HashMap<>();
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<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());
}
}
}
Output:
3, 1
4, 1
5, 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论