[英]how to compare two hash maps?
如何借助鍵比較兩個哈希圖中的值? 由於鍵是相同的,而值不是。 並返回每個鍵比較的布爾結果。 喜歡:
map1=[1,res]
[2,tr]
[3,677]
[4,cv]
map2=[1,res]
[2,cd]
[3,677]
[4,fs]
它應該還我
true
false
true
false
這是一種生成結果映射(將鍵映射為布爾值的映射)的方法。 無論鍵和鍵排序順序如何,它都能很好地播放:
/**
* Works with any two maps with common key / value types.
* The key type must implement Comparable though (for sorting).
* Returns a map containing all keys that appear in either of the supplied maps.
* The values will be true if and only if either
* - map1.get(key)==map2.get(key) (values may be null) or
* - map1.get(key).equals(map2.get(key)).
*/
public static <K extends Comparable<? super K>, V>
Map<K, Boolean> compareEntries(final Map<K, V> map1,
final Map<K, V> map2){
final Collection<K> allKeys = new HashSet<K>();
allKeys.addAll(map1.keySet());
allKeys.addAll(map2.keySet());
final Map<K, Boolean> result = new TreeMap<K, Boolean>();
for(final K key : allKeys){
result.put(key,
map1.containsKey(key) == map2.containsKey(key) &&
Boolean.valueOf(equal(map1.get(key), map2.get(key))));
}
return result;
}
private static boolean equal(final Object obj1, final Object obj2){
return obj1 == obj2 || (obj1 != null && obj1.equals(obj2));
}
用法:
public static void main(final String[] args){
final Map<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, null);
map1.put(2, "Different");
map1.put(3, "Same");
map1.put(4, "First Map only");
final Map<Integer, String> map2 = new HashMap<Integer, String>();
map2.put(3, "Same");
map2.put(1, null);
map2.put(2, "Yup, different");
map2.put(5, "Second Map only");
final Map<Integer, Boolean> comparisonResult =
compareEntries(map1, map2);
for(final Entry<Integer, Boolean> entry : comparisonResult.entrySet()){
System.out.println("Entry:" + entry.getKey() + ", value: "
+ entry.getValue());
}
}
輸出:
條目:1,值:true
條目:2,值:false
條目:3,值:true
條目:4,值:false
條目:5,值:false
我是從這個(可能是重復的)問題直接帶到這里的,很驚訝地發現答案中沒有提到Google Guava 。
不用重新發明輪子,而是使用方便的Maps.difference
方法。
假設您具有相同的條目集:
import java.util.HashMap;
import java.util.Map.Entry;
public class HashMapComparison {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, "res");
map1.put(2, "tr");
HashMap<Integer, String> map2 = new HashMap<Integer, String>();
map2.put(1, "res");
map2.put(2, "cd");
for (Entry<Integer, String> entry : map1.entrySet()) {
Integer key = entry.getKey();
System.out.println("key " + key + ": "
+ entry.getValue().equals(map2.get(key)));
}
}
}
偽代碼:
HashMap map1;
HashMap map2;
HashMap resultMap;
for(KeyType key : map1.keySet() ) {
if(map1.get(key).equals(map2.get(key)){
resultMap.put(key,true);
} else {
resultMap.put(key,false);
}
}
Map map = new HashMap();
map.put("AA","aaa");
map.put("BB","bbb");
map.put("CC","ccc");
map.put("DD","ddd");
Map map2 = new HashMap();
map2.put("BB","bbb");
map2.put("AA","aaa");
map2.put("DD","ddd");
map2.put("CC","ccc");
if(map.equals(map2))
{
System.out.println("Eql");
}
else
{
System.out.println("Not");
}
使用:
mapA.equals(mapB);
這將返回布爾結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.