简体   繁体   English

如何比较2个Maps Java的值

[英]How to compare the values of 2 Maps Java

At a high level, I'm trying to compare the values of 2 string maps 在较高的层次上,我正在尝试比较2个字符串映射的值

Map 1: [{ExpID1, SomeExpValue1}, {ExpID2, SomeExpValue2}, ...]

Map 2: [{FriID1, SomeFriValue1}, {FriID1, SomeFriValue2}, ...]

So ultimately, I'd like my comparison to be as such: 所以最终,我希望这样比较:

Sudo code: 须藤代码:

if ( ExpID1.equals(FriID1) ) {
    Assert.AssertEquals(SomeExpValue1, SomeFriValue1, "Library element does not match!")
}

However since these are Maps, some looping is required. 但是,由于这些是Map,因此需要进行一些循环。 My real question is here, how can I correctly and efficiently loop whitout having more a bunch of embedded foreach or forloops. 我真正的问题在这里,我如何正确而有效地循环使用更多的嵌入式foreach或forloops。 These seem to be super expensive. 这些似乎超级昂贵。 I feel my current looping is somehow wrong or unnessisarily expensive. 我觉得我当前的循环某种程度上是错误的或不必要地昂贵。 Any help or assistance would be much appreciated. 任何帮助或协助将不胜感激。

Currently what I have is this: 目前我所拥有的是:

@Test
public void testIntegration() throws Exception {

    // Get all files in directory
    File[] files = getFilesInFolder(friendlyNamesDir);

    String linkedResp = FileUtils.readFileToString(new File(explanationResponsePath));
    Map<String, String> map1 = getIDTitleMapExpResponse(linkedResp, "node");

    int counter = 1;
    for(File f : files) {
        if(f.length() == 0) throw new Exception("File does not exist...");


        for(String expID : map1.keySet()) {
            logger.info("Exp ID: "+expID);

            Map<String, String> map2 = getIDNameMapFromFriendly(f.getCanonicalPath());
            for(String friendlyID : map2.keySet()) {
                logger.info(counter+" - Friendly ID: "+friendlyID+" Name: "+map2.get(friendlyID));
                if(friendlyID.equals(expID)) {
                    Assert.assertEquals(map1.get(friendlyID), map2.get(expID));
                }
                counter++;
            }
        }
    }
}

As per Java documentation: 根据Java文档:
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#equals-java.lang.Object- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#equals-java.lang.Object-

You can compare maps directly, it will make sure to check for equality for elements in them. 您可以直接比较地图,这将确保检查其中的元素是否相等。

I understand that the maps are not necessarily equal, but must contain equal values for equal keys. 我知道映射不一定相等,但必须为相等的键包含相等的值。 Thus: 从而:

boolean alleq = true;
Set<String> key1 = new HashSet<>( map1.keySet() );
key1.retainAll( map2.keySet();
for( String s: key1 ) ){
     if( ! map1.get(s).equals( map2.get(s) ) ){
          alleq = false;
          break;
     }
}

You can also iterate one key set and test for the presence of the key in the other set; 您还可以迭代一个密钥集,并测试另一个密钥集中是否存在该密钥; there's no need to iterate the second set. 无需重复第二组。

boolean alleq = true;
for( String key1: map1.keySet() ){
    String value2 = map2.get( key1 );
    if( value2 != null && ! value2.equals( map1.get(key1) ) ){
        alleq = false;
        break;
    }
}

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

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