DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Difference Between HashMap and IdentityHashMap

Difference Between HashMap and IdentityHashMap

Most of the time I use HashMap whenever a map kinda object is needed. When reading some blog I came across IdentityHashMap in Java.

Himanshu Gupta user avatar by
Himanshu Gupta
·
Apr. 15, 10 · Opinion
Like (2)
Save
Tweet
Share
57.53K Views

Join the DZone community and get the full member experience.

Join For Free

Most of the time I use HashMap whenever a map kinda object is needed. When reading some blog I came across IdentityHashMap in Java. It is good to understand the differences between the two because you never know when you will see them flying across your code and you trying to find out why is  this kinda Map is used here.

IdentityHashMap as name suggests uses the equality operator(==) for comparing the keys. So when you put any Key Value pair in it the Key Object is compared using == operator.

 import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Map;public class IdentityMapDemo {public static void main(String[] args) { Map identityMap = new IdentityHashMap(); Map hashMap = new HashMap(); identityMap.put("a", 1); identityMap.put(new String("a"), 2); identityMap.put("a", 3);hashMap.put("a", 1); hashMap.put(new String("a"), 2); hashMap.put("a", 3);System.out.println("Identity Map KeySet Size :: " +  identityMap.keySet().size()); System.out.println("Hash Map KeySet Size :: " + hashMap.keySet().size()); } } 

On the other hand HashMap uses equals method to determine the uniqueness of the Key.

k1.equals(k2) 

instead of equality operator.

When you run the above code the result will be

Identity Map KeySet Size :: 2Hash Map KeySet Size :: 1

The Keysize of Identity Map is 2 because here a and new String(“a”) are considered two different Object. The comparison is done using == operator.

For HashMap the keySize is 1 because K1.equals(K2) returns true for all three Keys and hence it keep on removing the old value and updating it with the new one.

These both Maps will behave in same manner if they are used for Keys which are user defined Object and doesn’t overrides equals method.

From http://himanshugpt.wordpress.com/2010/03/24/difference-between-hashmap-and-identityhashmap/

Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Importance of Delegation in Management Teams
  • The Future of Cloud Engineering Evolves
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • DevOps Roadmap for 2022

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: