0% found this document useful (0 votes)
4 views2 pages

Java AbstractMap Equals Method - GeeksforGeeks

The Java AbstractMap class's equals() method checks the equality of two maps by comparing their key-value pairs. It returns true if the specified object is equal to the current map, otherwise false. An example demonstrates the usage of this method with different HashMap instances.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java AbstractMap Equals Method - GeeksforGeeks

The Java AbstractMap class's equals() method checks the equality of two maps by comparing their key-value pairs. It returns true if the specified object is equal to the current map, otherwise false. An example demonstrates the usage of this method with different HashMap instances.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Search...

Tutorials
Practice J
Jobs
Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Colle

Java AbstractMap equals() Method


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() Method

public boolean equals(Object o)

Parameter: The Object to be compared with the current map.


Return Type: Return "true" if the specified object is equal to the current map, otherwise return
"false".

Example: This example demonstrates how the equals() method checks for equality between
different HashMap instances based on their key-value mapping.

// Java code to demonstrate the working of equals()


import [Link];
import [Link];
import [Link];

public class Geeks {


public static void main(String[] args)
{
// Creating three HashMap instances
Map<String, Integer> hm1 = new HashMap<>();
[Link]("Geek1", 1);
[Link]("Geek2", 2);
[Link]("Geek3", 3);

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


[Link]("Geek3", 3);
[Link]("Geek2", 2);
[Link]("Geek1", 1);

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


[Link]("Geek1", 1);
[Link]("Geek2", 2);
[Link]("Geek4", 4);

[Link]("First HashMap: " + hm1);


[Link]("Second HashMap: " + hm2);
[Link]("Third HashMap: " + hm3);

// Checking equality between the maps


[Link]("Is hm1 equal to hm2? " + [Link](hm2));
[Link]("Is hm1 equal to hm3? " + [Link](hm3));

}
}

Output

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

Suggested Quiz 4 Questions

What will happen if you try to add a null key in a HashMap?

A Throws NullPointerException

B Allowed only if the value is non-null

C Allowed, but only once

D Allowed multiple times

View Explanation 1/4 < Previous Next >

Comment K chinm…

Article Tags: Misc Java Java-Collections Java - util package Java-HashMap +2 More

Company Explore Tutorials Courses Preparation


About Us POTD Programming ML and Data Corner
Corporate & Communications Address: Legal Practice Languages Science Interview
Privacy Problems DSA DSA and Corner
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Policy Connect Web Placements Aptitude
Pradesh (201305) Careers Blogs Technology Web Puzzles
Contact Us Upskill AI, ML & Development GfG 160
Registered Address:
Corporate Courses Data Science Data Science System Design
K 061, Tower K, Gulshan Vivante Solution DevOps Programming
Apartment, Sector 137, Noida, Gautam
CS Core Languages
Buddh Nagar, Uttar Pradesh, 201305
Subjects

You might also like