How can we compare two hashmaps in java

My application stores the values ​​from the database and user values ​​and compares them. To save these values, I used hashmap with the key as an integer and listed as data and saved the values. It is not necessary that the values ​​and key data be the same, but all data values ​​in the two hash maps must be the same

Symbols: key: meaning

1-[cd,done]
2-[mb,done]
3-[core,done]

another -

1-[mb,done]
2-[core,done]
3-[cd,done]

In the code, the above hasmap should be equal regardless of the order in which the values ​​are stored, but at the moment I can not encode it correctly ...

+3
source share
3 answers

As I understand it, you just want to check that the values ​​of the cards are not equal to the cards themselves.

, values() containsAll:

hashMap1.values().containsAll(hashMap2.values()) &&
    hashMap2.values().containsAll(hashMap1.values());

CollectionUtils.isEqualCollection :

CollectionUtils.isEqualCollection(hashMap1.values(), hashMap2.values());
+2

keySet() .

for (Map.Entry<String, String> me : hashmap1.entrySet()) // assuming your map is Map<String, String>  
        if(hashmap2.containsKey(me.getKey()))  
            System.out.println("Found Duplicate -> " + me.getKey())
+1

public static void main (String [] args) {

    Map<Integer,String> map1 = new HashMap<Integer,String>();
    Map<Integer,String> map2 = new HashMap<Integer,String>();

    map1.put(1, "ABC");
    map1.put(2, "1123");
    map2.put(1, "XYZ");

    boolean val = map1.keySet().containsAll(map2.keySet());

    for (Entry<Integer, String> str : map1.entrySet()) {
        System.out.println("================="+str.getKey());

        if(map2.containsKey(str.getKey())){
            System.out.println("Map2 Contains Map 1 Key");
        }
    }

    System.out.println("================="+val);
}
0
source

All Articles