This example shows how to compare two HashMap in Java using the equals method. The example also shows how to compare HashMap keys or values with another HashMap.
How to compare two HashMap objects in Java using the equals method?
The equals
method of the Map compares two map objects for equality. It returns true if the mappings of both the map are the same.
1 |
boolean equals(Object o) |
In other words, the equals
method of the Map returns true if and only if the maps have the same key-value pairs.
The below given example shows how to compare the map objects using the equals
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.HashMap; public class HashMapCompareExample { public static void main(String[] args) { HashMap<Integer, String> hmap1 = new HashMap<Integer, String>(); hmap1.put(1, "One"); hmap1.put(2, "Two"); hmap1.put(3, "Three"); HashMap<Integer, String> hmap2 = new HashMap<Integer, String>(); hmap2.put(3, "Three"); hmap2.put(1, "One"); hmap2.put(2, "Two"); /* * To compare two HashMaps, use the * equals method */ //returns true as both HashMap objects have same mappings System.out.println( hmap1.equals(hmap2) ); //remove one mapping from the first map hmap1.remove(2); //returns false as both HashMap objects does not have same mappings System.out.println( hmap1.equals(hmap2) ); } } |
Output
1 2 |
true false |
Important Note: The order of the HashMap mappings does not have to be the same as you can see from the above example.
How to compare two HashMap keys?
We can check if two HashMap objects have the same keys by comparing their keys obtained using the keySet
method. The keySet method returns a Set view of all keys contained in the HashMap.
We can use the equals
method of the Set to compare the keys as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
HashMap<Integer, String> hmap1 = new HashMap<Integer, String>(); hmap1.put(1, "One"); hmap1.put(2, "Two"); hmap1.put(3, "Three"); HashMap<Integer, String> hmap2 = new HashMap<Integer, String>(); hmap2.put(3, "Three"); hmap2.put(1, "One"); hmap2.put(2, "Two"); /* * To compare HashMap keys, get all keys * of both the map using the keySet method and * then compare them using the equals method */ //returns true System.out.println( hmap1.keySet().equals( hmap2.keySet() ) ); hmap1.remove(2); //returns false System.out.println( hmap1.keySet().equals( hmap2.keySet() ) ); |
Output
1 2 |
true false |
How to compare HashMap values?
We can compare if values contained in the map objects are the same or not by converting all map values to Set and then use the equals
method of the Set as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
HashMap<Integer, String> hmap1 = new HashMap<Integer, String>(); hmap1.put(1, "One"); hmap1.put(2, "Two"); hmap1.put(3, "Three"); HashMap<Integer, String> hmap2 = new HashMap<Integer, String>(); hmap2.put(3, "Three"); hmap2.put(1, "One"); hmap2.put(2, "Two"); /* * Get all map values using the values method, * convert them to a Set */ Set<String> valueSet1 = new HashSet<String>( hmap1.values() ); Set<String> valueSet2 = new HashSet<String>( hmap2.values() ); //compare using Set.equals method System.out.println( valueSet1.equals(valueSet2) ); |
Output
1 |
true |
Important Note: The Set is a collection of unique values. If your HashMap contains duplicate values, the above approach will remove all duplicates values and only checks that the values contained in both the map objects are equal.
It might be possible that one map contains one value mapped to many different keys while the other does not. In that case, even if the map objects are different, the above approach returns true for values.
All of the above given comparisons work if and only if the key and value objects have implemented the equals
methods. If your HashMap has custom class objects either as keys or values, the custom class has to implement the equals
and hashCode
methods.
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.