简体   繁体   中英

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.

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:

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

You can use Stream API. Filter the non-matched value of map using filter then print the values using 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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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