Last Updated : 11 Jul, 2025
The equals() method of the Java AbstractMap class is used to check equality between two maps. It compares the key-value pair in the current map with the key-value pair of the specified map.
Syntax of AbstarctMap equals() Methodpublic boolean equals(Object o)
Example: This example demonstrates how the equals() method checks for equality between different HashMap instances based on their key-value mapping.
Java
// Java code to demonstrate the working of equals()
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
public class Geeks {
public static void main(String[] args)
{
// Creating three HashMap instances
Map<String, Integer> hm1 = new HashMap<>();
hm1.put("Geek1", 1);
hm1.put("Geek2", 2);
hm1.put("Geek3", 3);
Map<String, Integer> hm2 = new HashMap<>();
hm2.put("Geek3", 3);
hm2.put("Geek2", 2);
hm2.put("Geek1", 1);
Map<String, Integer> hm3 = new HashMap<>();
hm3.put("Geek1", 1);
hm3.put("Geek2", 2);
hm3.put("Geek4", 4);
System.out.println("First HashMap: " + hm1);
System.out.println("Second HashMap: " + hm2);
System.out.println("Third HashMap: " + hm3);
// Checking equality between the maps
System.out.println("Is hm1 equal to hm2? " + hm1.equals(hm2));
System.out.println("Is hm1 equal to hm3? " + hm1.equals(hm3));
}
}
First HashMap: {Geek3=3, Geek2=2, Geek1=1} Second HashMap: {Geek3=3, Geek2=2, Geek1=1} Third HashMap: {Geek4=4, Geek2=2, Geek1=1} Is hm1 equal to hm2? true Is hm1 equal to hm3? false
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4