如何比较两个散列映射?

时间:2023-02-10 02:12:23

How to compare the values in both hash maps with the help of keys ? Since the keys are identical whereas values are'nt. and return boolean result for each key comparision. like:

如何在键的帮助下比较两个散列映射中的值?因为键是相同的,而值是'nt。并返回每个键比较的布尔结果。如:

map1=[1,res]
[2,tr]
[3,677]
[4,cv]  

map2=[1,res]
[2,cd]
[3,677]
[4,fs]

It should return me

它应该返回我

true
false
true
false

7 个解决方案

#1


9  

Here's a method that generates a Map of the results (Map of key to boolean). It will play nicely regardless of different keys and key sort order:

这里有一个生成结果映射的方法(从key映射到boolean)。无论不同的键和键的排序顺序是什么,它都会表现得很好:

/**
 * 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));
}

Usage:

用法:

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());
    }

}

Output:

输出:

Entry:1, value: true
Entry:2, value: false
Entry:3, value: true
Entry:4, value: false
Entry:5, value: false

条目:1,value: true Entry:2, value: false Entry:3, value: true Entry:4, value: false Entry:5, value: false

#2


5  

I was directed here from this (possibly duplicate) question and was surprised to find that Google Guava wasn't mentioned in the answers.

我从这个(可能是重复的)问题中得到指示,惊讶地发现在答案中没有提到谷歌Guava。

Instead of re-inventing the wheel, use the handy the Maps.difference method.

与其重新发明*,不如使用方便的地图。差分法。

#3


3  

Assuming that you have the same entry sets:

假设你有相同的入口集:

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)));
        }

    }

}

#4


2  

You can take keys of one map (with keySet() method), iterate through them, get values from both maps (with get() method), and compare them.

您可以使用一个map的键(使用keySet()方法),遍历它们,从两个映射中获取值(使用get()方法),并比较它们。

Also, you can use values() method to get all key-value pairs form one map, iterate through it and compare values with values from another map.

此外,还可以使用values()方法从一个映射中获取所有键-值对,并对其进行迭代,并将值与另一个映射中的值进行比较。

#5


1  

Pseudo Code:

伪代码:

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);
  }   

}

}

#6


1  

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");
}

#7


0  

use:

使用:

mapA.equals(mapB);

This will give back a boolean result.

这将返回一个布尔结果。

#1


9  

Here's a method that generates a Map of the results (Map of key to boolean). It will play nicely regardless of different keys and key sort order:

这里有一个生成结果映射的方法(从key映射到boolean)。无论不同的键和键的排序顺序是什么,它都会表现得很好:

/**
 * 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));
}

Usage:

用法:

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());
    }

}

Output:

输出:

Entry:1, value: true
Entry:2, value: false
Entry:3, value: true
Entry:4, value: false
Entry:5, value: false

条目:1,value: true Entry:2, value: false Entry:3, value: true Entry:4, value: false Entry:5, value: false

#2


5  

I was directed here from this (possibly duplicate) question and was surprised to find that Google Guava wasn't mentioned in the answers.

我从这个(可能是重复的)问题中得到指示,惊讶地发现在答案中没有提到谷歌Guava。

Instead of re-inventing the wheel, use the handy the Maps.difference method.

与其重新发明*,不如使用方便的地图。差分法。

#3


3  

Assuming that you have the same entry sets:

假设你有相同的入口集:

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)));
        }

    }

}

#4


2  

You can take keys of one map (with keySet() method), iterate through them, get values from both maps (with get() method), and compare them.

您可以使用一个map的键(使用keySet()方法),遍历它们,从两个映射中获取值(使用get()方法),并比较它们。

Also, you can use values() method to get all key-value pairs form one map, iterate through it and compare values with values from another map.

此外,还可以使用values()方法从一个映射中获取所有键-值对,并对其进行迭代,并将值与另一个映射中的值进行比较。

#5


1  

Pseudo Code:

伪代码:

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);
  }   

}

}

#6


1  

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");
}

#7


0  

use:

使用:

mapA.equals(mapB);

This will give back a boolean result.

这将返回一个布尔结果。