🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In the Java collections framework, HashMap is a part of the standard map implementations that associates keys to values. IdentityHashMap, on the other hand, is a special implementation of the Map interface that uses reference equality when comparing keys and values. In simpler terms, IdentityHashMap checks if key-value pairs are identical in terms of their memory address, not merely their contents.
2. Key Points
1. HashMap uses the .equals() method to determine key equality, which checks if two keys are logically equal.
2. IdentityHashMap uses == for key comparison, which checks if the key references point to the same object.
3. Performance-wise, IdentityHashMap can be faster in scenarios where reference equality is used because it avoids the potentially costly .equals() method.
4. IdentityHashMap is not a widely-used Map implementation and is designed for specific use cases, such as serialization or deep equality checks.
3. Differences: IdentityHashMap vs HashMap in Java
| HashMap | IdentityHashMap |
|---|---|
| Uses .equals() method for key comparison. | Uses == operator for key comparison. |
| General-purpose implementation of Map. | Specialized implementation of Map for identity-based key comparison. |
| More commonly used in applications. | Less commonly used; suitable for specific use cases. |
4. Example
// Step 1: Import the necessary classes
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
public class MapComparison {
public static void main(String[] args) {
// Step 2: Create a HashMap
Map<String, String> hashMap = new HashMap<>();
// Step 3: Create an IdentityHashMap
Map<String, String> identityMap = new IdentityHashMap<>();
String keyA = new String("key");
String keyB = new String("key");
// Step 4: Put the same value with two different key references into the HashMap
hashMap.put(keyA, "value");
hashMap.put(keyB, "value");
// Step 5: Put the same value with two different key references into the IdentityHashMap
identityMap.put(keyA, "value");
identityMap.put(keyB, "value");
// Step 6: Print the size of both maps
System.out.println("Size of HashMap: " + hashMap.size());
System.out.println("Size of IdentityHashMap: " + identityMap.size());
}
}
Output:
Size of HashMap: 1 Size of IdentityHashMap: 2
Explanation:
1. HashMap and IdentityHashMap objects are instantiated.
2. Two distinct key objects with identical contents are created.
3. Both keys are associated with the same value and inserted into the HashMap, but since HashMap uses .equals(), it perceives both keys as equal, resulting in a size of 1.
4. The same keys are inserted into the IdentityHashMap, which uses == for key comparison. Since the key references are not the same, IdentityHashMap considers them as unique keys, hence a size of 2.
5. This demonstrates the identity-based comparison of IdentityHashMap versus the content-based comparison of HashMap.
5. When to use?
- Use HashMap for most applications where a map is needed, and you expect keys to be compared based on logical equality.
- Opt for IdentityHashMap when you need to key off the identity of objects, such as when dealing with object serialization, topology-preserving object graph transformations, or maintaining proxy objects.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment