Compare Two HashMap Objects in Java
In this article, we will learn how to compare two HashMap objects in Java. In java.util package HashMap class is present. HashMap is used to store key-value pairs, so there are different scenarios to compare two Objects of HashMap. Which is the following:
- Compare Entry
- Compare Keys
- Compare Values
Example:
Input : HashMapA = [a=1, b=2], HashMapB = [a=1, c=2]
Output:
Compare Entry = Not Equal
Compare Keys = Not Equal
Compare values = Equal
1. Compare Entry:
Entry is a key-value pair. We can compare two HashMap by comparing Entry with the equals() method of the Map returns true if the maps have the same key-value pairs that mean the same Entry.
Below is the implementation:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<Integer, String> map1 = new HashMap<>();
map1.put( 1 , "Akshay" );
map1.put( 2 , "Bina" );
map1.put( 3 , "Chintu" );
HashMap<Integer, String> map2 = new HashMap<>();
map2.put( 3 , "Chintu" );
map2.put( 2 , "Bina" );
map2.put( 1 , "Akshay" );
HashMap<Integer, String> map3 = new HashMap<>();
map3.put( 1 , "Akshay" );
map3.put( 2 , "Binod" );
map3.put( 3 , "Chintu" );
System.out.println( "map1 == map2 : "
+ map1.equals(map2));
System.out.println( "map1 == map3 : "
+ map1.equals(map3));
}
}
|
Output
map1 == map2 : true
map1 == map3 : false
2. Compare Keys:
We can check if two HashMap objects have the same keys by comparing their keys obtained using the keySet() method. We use equals() method of the set to compare keys.
Below is the implementation:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<Integer, String> map1 = new HashMap<>();
map1.put( 1 , "Akshay" );
map1.put( 2 , "Bina" );
map1.put( 3 , "Chintu" );
HashMap<Integer, String> map2 = new HashMap<>();
map2.put( 3 , "Chintu" );
map2.put( 2 , "Bina" );
map2.put( 1 , "Akshay" );
HashMap<Integer, String> map3 = new HashMap<>();
map3.put( 1 , "Akshay" );
map3.put( 2 , "Binod" );
map3.put( 4 , "Chintu" );
System.out.println(
"map1 keys == map2 keys : "
+ map1.keySet().equals(map2.keySet()));
System.out.println(
"map1 keys == map3 keys : "
+ map1.keySet().equals(map3.keySet()));
}
}
|
Output
map1 keys == map2 keys : true
map1 keys == map3 keys : false
3. Compare Values:
We can compare if values contained in the map objects are the same or not by converting all map values to set using values() method and then compare values with the equals() method of the set.
Below is the implementation:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<Integer, String> map1 = new HashMap<>();
map1.put( 1 , "Akshay" );
map1.put( 2 , "Bina" );
map1.put( 3 , "Chintu" );
HashMap<Integer, String> map2 = new HashMap<>();
map2.put( 3 , "Chintu" );
map2.put( 2 , "Bina" );
map2.put( 1 , "Akshay" );
HashMap<Integer, String> map3 = new HashMap<>();
map3.put( 1 , "Akshay" );
map3.put( 2 , "Binod" );
map3.put( 3 , "Shinchain" );
HashSet<String> set1 = new HashSet<>(map1.values());
HashSet<String> set2 = new HashSet<>(map2.values());
HashSet<String> set3 = new HashSet<>(map3.values());
System.out.println( "map1 values == map2 values : "
+ set1.equals(set2));
System.out.println( "map1 values == map3 values : "
+ set1.equals(set3));
}
}
|
Output
map1 values == map2 values : true
map1 values == map3 values : false
Last Updated :
04 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...