Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaCompare Two HashMap Objects in Java

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:

  1. Compare Entry
  2. Compare Keys
  3. 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




// Java program to compare two HashMap objects
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // New HashMap1
        HashMap<Integer, String> map1 = new HashMap<>();
  
        // Add Entry to map
        map1.put(1, "Akshay");
        map1.put(2, "Bina");
        map1.put(3, "Chintu");
  
        // New HashMap2
        HashMap<Integer, String> map2 = new HashMap<>();
  
        // Add Entry to map
        map2.put(3, "Chintu");
        map2.put(2, "Bina");
        map2.put(1, "Akshay");
  
        // New HashMap3
        HashMap<Integer, String> map3 = new HashMap<>();
  
        // Add Entry to map
        map3.put(1, "Akshay");
        map3.put(2, "Binod");
        map3.put(3, "Chintu");
  
        // Compare map1 and map2
        System.out.println("map1 == map2 : "
                           + map1.equals(map2));
  
        // Compare map1 and map3
        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




// Java program to compare two HashSet keys
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // New HashMap1
        HashMap<Integer, String> map1 = new HashMap<>();
  
        // Add Entry to map
        map1.put(1, "Akshay");
        map1.put(2, "Bina");
        map1.put(3, "Chintu");
  
        // New HashMap2
        HashMap<Integer, String> map2 = new HashMap<>();
  
        // Add Entry to map
        map2.put(3, "Chintu");
        map2.put(2, "Bina");
        map2.put(1, "Akshay");
  
        // New HashMap3
        HashMap<Integer, String> map3 = new HashMap<>();
  
        // Add Entry to map
        map3.put(1, "Akshay");
        map3.put(2, "Binod");
        map3.put(4, "Chintu");
  
        // Compare map1 and map2 keys using keySet() and
        // equals() method
        System.out.println(
            "map1 keys == map2 keys : "
            + map1.keySet().equals(map2.keySet()));
  
        // Compare map1 and map3 keys using keySet() and
        // equals() method
        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




// Java program to compare HashMap values
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // New HashMap1
        HashMap<Integer, String> map1 = new HashMap<>();
  
        // Add Entry to map
        map1.put(1, "Akshay");
        map1.put(2, "Bina");
        map1.put(3, "Chintu");
  
        // New HashMap2
        HashMap<Integer, String> map2 = new HashMap<>();
  
        // Add Entry to map
        map2.put(3, "Chintu");
        map2.put(2, "Bina");
        map2.put(1, "Akshay");
  
        // New HashMap3
        HashMap<Integer, String> map3 = new HashMap<>();
  
        // Add Entry to map
        map3.put(1, "Akshay");
        map3.put(2, "Binod");
        map3.put(3, "Shinchain");
  
        // Value set of map1
        HashSet<String> set1 = new HashSet<>(map1.values());
  
        // Value set of map2
        HashSet<String> set2 = new HashSet<>(map2.values());
  
        // Value set of map3
        HashSet<String> set3 = new HashSet<>(map3.values());
  
        // Compare map1 and map2 values using equals()
        // method
        System.out.println("map1 values == map2 values : "
                           + set1.equals(set2));
  
        // Compare map1 and map3 values using equals()
        // method
        System.out.println("map1 values == map3 values : "
                           + set1.equals(set3));
    }
}


Output

map1 values == map2 values : true
map1 values == map3 values : false
RELATED ARTICLES

Most Popular

Recent Comments