Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare two hash maps?

Tags:

java

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:

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
like image 704
reshu Avatar asked Jan 04 '11 09:01

reshu


People also ask

Can we equate two Hashmaps?

If we want to compare hashmaps by keys i.e. two hashmaps will be equals if they have exactly same set of keys, we can use HashMap. keySet() function. It returns all the map keys in HashSet. We can compare the hashset of keys for both maps using Set.

How do I compare two maps in groovy?

With Groovy 1.8 the equals() method is added to Map . This means we can check if maps are equals. They are equals if both maps have the same size, and keys and values are the same.

How can I compare two maps in C++?

C++ Map Library - operator== Functionb The C++ function std::map::operator== tests whether two maps are equal or not.


2 Answers

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:

/**
 * 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

like image 169
Sean Patrick Floyd Avatar answered Oct 05 '22 06:10

Sean Patrick Floyd


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

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

like image 28
Tim Pote Avatar answered Oct 05 '22 07:10

Tim Pote