简体   繁体   English

比较两个Java映射的键和值

[英]Comparing key and values of two java maps

I'm still relatively new to Java and I'm finding myself stuck on trying to properly write this piece of code that I feel should be a bit more simple. 我对Java还是一个相对较新的人,发现自己只能尝试正确地编写这段我认为应该更简单的代码。

I have two maps made up of two different instances of the same object. 我有两个由同一对象的两个不同实例组成的地图。 The keys are objects, and the values are objects. 键是对象,值是对象。

There are two instances because I'm trying to determine if the keys in one instance are different from the keys in another instance. 有两个实例,因为我试图确定一个实例中的键与另一个实例中的键是否不同。 I'm trying to specifically locate new keys or missing keys, and then compare the values of the keys which exist in both maps. 我正在尝试专门定位新键或丢失的键,然后比较两个地图中都存在的键的值。

The sample code below is just to help visualize what I'm trying to do (hopefully it's not more confusing!) 下面的示例代码只是为了帮助可视化我正在尝试做的事情(希望它不会更加令人困惑!)

The goal of the below example should tell me that key "C" is missing and there is a new key ("D") and then it should finally compare the values of keys in both maps. 以下示例的目标应该告诉我,键“ C”丢失了,并且有一个新键(“ D”),然后最终应该比较两个映射中的键值。

Main question is, is there anyway to do this in one loop? 主要问题是,是否有一个循环进行此操作? Primarily because my actual code will touch the file system for the values in the map and I'm trying to minimize the times it has to touch the disk 主要是因为我的实际代码将接触文件系统以获取映射中的值,并且我试图将其接触磁盘的时间减至最少

Map<objA, objB> mapA = new HashMap<objA, objB>();
mapA.put("A", "1");
mapA.put("B", "2");
mapA.put("C", "3");

Map<objA, objB> mapB = new HashMap<objA, objB>();
mapB.put("A", "1");
mapB.put("D", "4");

// Check if something is missing from mapB
for(Map.Entry<objA, objB> entryMapA:mapA.entrySet())
{
    if(!mapB.containsKey(entryMapA.getKey())
        {
            System.out.println(entryMapA.getKey() + " is missing");
        }
}

// Check if something is new is in mapB
for(Map.Entry<objA, objB> entryMapB:mapB.entrySet())
{
    if(!mapA.containsKey(entryMapB.getKey())
    {  
        System.out.println(entryMapB.getKey() + " is new");
    }
}

Keys in a Map are Set s, so you can use sets and the available operations on them. Map中的键是Set ,因此您可以使用set及其上的可用操作。

For instance: 例如:

Set<String> keysInA = new HashSet<String>(mapA.keySet());
Set<String> keysInB = new HashSet<String>(mapB.keySet());

// Keys in A and not in B
Set<String> inANotB = new HashSet<String>(keysInA);
inANotB.removeAll(keysInB);

// Keys common to both maps
Set<String> commonKeys = new HashSet<String>(keysInA);
commonKeys.retainAll(keysInB);

etc etc. 等等等

Note: you MUST NOT use the key set of a map directly. 注意:您绝对不能直接使用地图的键集。 If you do: 如果您这样做:

// This returns the actual key set of the map, NOT a copy!
Set<String> inANotB = mapA.keysSet();
inANotB.removeAll(mapB.keySet())

you actually remove the keys (and their associated values) in mapA . 您实际上删除了mapA中的键(及其关联的值)

Finally, you should note that HashSet makes no order guarantee. 最后,您应该注意, HashSet不保证任何顺序。 If this matters to you, you want to look at implementations of SortedSet (such as TreeSet ). 如果这对您很重要,则需要查看SortedSet实现(例如TreeSet )。

You may subtract the the keysets: 您可以减去键集:

Set<objA> keysA1 = new HashSet<objA>(mapA.keySet()); // deepcopy
Set<objA> keysA2 = new HashSet<objA>(mapA.keySet()); // deepcopy
Set<objB> keysB = new HashSet<objB>(mapB.keySet()); // deepcopy

keysA1.removeAll(keysB);
keysB.removeAll(keysA2);

System.out.println("Missing in A: " + keysB);
System.out.println("Missing in B: " + keysA1);

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

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