英文:
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<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?
答案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<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
答案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 -> !e.getValue().equals(dstMap.get(e.getKey())))
.forEach(e -> System.out.println(e.getValue() + " "+ 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<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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论