简体   繁体   English

在 Java 中区分和比较映射值的有效方法

[英]Efficient way to differentiate and compare Map Values in Java

I have 2 LinkedHashMap and i am trying to compare each other and should display if any differences in values based on key.我有 2 个 LinkedHashMap,我正在尝试相互比较,并且应该显示基于键的值是否有任何差异。

srcMap={Col1=Val1,Col2=Val2,Col3=Val3,Col4=ValDiff,,Col5=Val3}
dstMap={Col1=Val1,Col2=Val2,Col3=Val3,Col4=Val3,,Col5=ValMisMatch}

I am trying below way using loop.我正在尝试以下方式使用循环。

Iterator<Map.Entry<String,String>> itSrc=srcMap.entrySet().iterator();
while(itSrc.hasNext()){
String srcKey=itSrc.next().getKey();
String srcValue=srcMap.get(srcKey);
String dstValue=dstMap.get(srcKey);
if(!srcValue.equals(dstValue))
  //printing value and corresponding key
}

What I am looking for is, Is there any better/faster way than this?我正在寻找的是,有没有比这更好/更快的方法?

I assume something like this would be fancy我认为这样的事情会很花哨

for(Map.Entry<String,String> entry : srcMap.entrySet()) {
    String key = entry.getKey();
    if (dstMap.contains(key)) {
        if (entry.getValue().equals(dstMap.get(key)) {
            continue;
        }
    }

    System.out.println("Your desired output");
}

UPD.更新。 If your program does not expect that someone mapped null to some key:如果您的程序不希望有人将null映射到某个键:

for(Map.Entry<String,String> entry : srcMap.entrySet()) {
    String key = entry.getKey();
    
    if (entry.getValue().equals(dstMap.get(key)) {
        continue;
    }

    System.out.println(
        "Key:" + key + 
        "; Values differ:" + entry.getValue() + 
        "," + dstMap.get(key)
    );
}

Note that map.get(key) may return null in two cases - someone mapped null to the key, or the value for the key is not present请注意map.get(key)在两种情况下可能返回 null - 有人将 null 映射到键,或者键的值不存在

You can use Stream API.您可以使用流 API。 Filter the non-matched value of map using filter then print the values using forEach使用filter地图的不匹配值,然后使用forEach打印值

srcMap.entrySet()
      .stream()
      .filter(e -> !e.getValue().equals(dstMap.get(e.getKey())))
      .forEach(e -> System.out.println(e.getValue() + " "+ dstMap.get(e.getKey())));

You could use Guava:你可以使用番石榴:

https://guava.dev/releases/20.0/api/docs/com/google/common/collect/MapDifference.html https://guava.dev/releases/20.0/api/docs/com/google/common/collect/MapDifference.html

For example if例如如果

import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

Map<String,String> srcMap = new LinkedHashMap<>();
srcMap.put("Col1","Val1");
srcMap.put("Col2","Val2");
srcMap.put("Col3","Val3");
srcMap.put("Col4","ValDiff");
srcMap.put("Col5","Val3");

Map<String,String> dstMap = new LinkedHashMap<>();
dstMap.put("Col1","Val1");
dstMap.put("Col2","Val2");
dstMap.put("Col3","Val3");
dstMap.put("Col4","Val3");
dstMap.put("Col5","ValMisMatch");

MapDifference<String, String> diff = Maps.difference(srcMap, dstMap);
System.out.println("All Differences: " + diff.entriesDiffering());
System.out.println("All Common Entries: " + diff.entriesInCommon());

Would get you:会让你:

All Differences: {Col4=(ValDiff, Val3), Col5=(Val3, ValMisMatch)}
All Common Entries: {Col1=Val1, Col2=Val2, Col3=Val3}

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

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