在Java中区分和比较Map值的高效方法

huangapple go评论86阅读模式
英文:

Efficient way to differentiate and compare Map Values in Java

问题

我有两个LinkedHashMap,我试图将它们进行比较,如果基于键有任何值上的差异,则应该显示出来。

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

我正在尝试以下循环方法。

    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))
      //打印值和对应的键
    }

我想知道的是,有没有比这更好/更快的方法?
英文:

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&lt;Map.Entry&lt;String,String&gt;&gt; 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?

答案1

得分: 2

以下是翻译好的代码部分:

我假设类似这样的代码会很漂亮

    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("您期望的输出");
    }

**更新. 如果您的程序不期望有人将`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 + 
            ";值不同:" + entry.getValue() + 
            "," + dstMap.get(key)
        );
    }

请注意,`map.get(key)` 在两种情况下可能返回null - 有人将null映射到键或者键的值不存在
英文:

I assume something like this would be fancy

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

    System.out.println(&quot;Your desired output&quot;);
}

UPD. If your program does not expect that someone mapped null to some key:

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

    System.out.println(
        &quot;Key:&quot; + key + 
        &quot;; Values differ:&quot; + entry.getValue() + 
        &quot;,&quot; + 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

答案2

得分: 1

可以使用Stream API。使用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 can use Stream API. Filter the non-matched value of map using filter then print the values using forEach

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

答案3

得分: 1

你可以使用Guava库:

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

例如,如果有以下代码:

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("所有不同之处:" + diff.entriesDiffering());
System.out.println("所有共同条目:" + diff.entriesInCommon());

会得到以下结果:

所有不同之处:{Col4=(ValDiff, Val3), Col5=(Val3, ValMisMatch)}
所有共同条目:{Col1=Val1, Col2=Val2, Col3=Val3}
英文:

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&lt;String,String&gt; srcMap = new LinkedHashMap&lt;&gt;();
srcMap.put(&quot;Col1&quot;,&quot;Val1&quot;);
srcMap.put(&quot;Col2&quot;,&quot;Val2&quot;);
srcMap.put(&quot;Col3&quot;,&quot;Val3&quot;);
srcMap.put(&quot;Col4&quot;,&quot;ValDiff&quot;);
srcMap.put(&quot;Col5&quot;,&quot;Val3&quot;);

Map&lt;String,String&gt; dstMap = new LinkedHashMap&lt;&gt;();
dstMap.put(&quot;Col1&quot;,&quot;Val1&quot;);
dstMap.put(&quot;Col2&quot;,&quot;Val2&quot;);
dstMap.put(&quot;Col3&quot;,&quot;Val3&quot;);
dstMap.put(&quot;Col4&quot;,&quot;Val3&quot;);
dstMap.put(&quot;Col5&quot;,&quot;ValMisMatch&quot;);

MapDifference&lt;String, String&gt; diff = Maps.difference(srcMap, dstMap);
System.out.println(&quot;All Differences: &quot; + diff.entriesDiffering());
System.out.println(&quot;All Common Entries: &quot; + diff.entriesInCommon());

Would get you:

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

huangapple
  • 本文由 发表于 2020年8月27日 19:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63615221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定