IdentityHashMap Class in Java8 May 2025 | 8 min read The IdentityHashMap class is similar to the HashMap class. It implements the AbstractMap class. However, it uses reference equality rather than object equality while comparing the key (or values). It is not the general purpose implementation of Map. While this class implements the Map interface, it intentionally violates Map's general contract that mandates the use of the equals() method when comparing objects. It is designed especially for the rare cases where the reference-equality semantics are needed. It is used when the user needs the objects to be compared using the reference. We need to import java.util package to use IdentityHashMap class. Features of IdentityHashMap
Syntax: where, K is the key of Object type and V is the value of Object type. Constructors of IdentityHashMapThere are two ways to create instance of IdentityHashMap: OR 1. IdentityHashMap(): It creates a new and empty identity hash map with the default expected maximum size that is 21. Syntax: 2. IdentityHashMap(int ExpectedMaxSize): It creates a new and empty identity hash map with the given specified expected maximum size. Syntax: 3. IdentityHashMap(Map m): It creates a new identity hash map with the key-value pairs given in the specified map. Syntax: Methods of the IdentityHashMap ClassAlong with the methods inherited from its parent classes, IdentityHashMap defines following methods:
Let's use the methods of the IdentityHashMap class. IdentityHashMapDemo.java Output: ![]() Basic Operations on IdentityHashMap1. Adding the elementsTo add elements into the IdentityHashMap, we have use the put() and putAll() methods. The put() method enters the specified key and value mapped together in the map. When the current key is passed, previous value is replaced by new value. The putAll() method copies all the key value mappings from one map to another. Example: Let's consider following example where we implement put() and putAll() methods in a Java program. IdentityHashMapExample1.java Output: ![]() 2. Removing the elementsTo delete the mappings (key-value pairs) from the map, we use the remove() method. Let's consider following example where we use remove() method to delete a mapping in Java program. IdentityHashMapExample2.java Output: ![]() 3. Accessing the elementsTo access the elements of an IdentityHashMap we use the get()method. Let's consider following example where we use get() method to access the mappings from the map. IdentityHashMapExample3.java Output: ![]() 4. Traversing through IdentityHashMapTo traverse over the all the structures of Collection Framework, we use Iterator interface. As the Iterators work with only single type of data, we use Entry IdentityHashMapExample4.java Output: ![]() Difference Between IdentityHashMap and HashMap
The following example explains the difference between IdentityHashMap and HashMap classes. IdentityHashMapVSHashMap.java Output: ![]() Synchronized IdentityHashMapWhen more than one threads access the identity hash map concurrently, and at least one of the threads structurally modifies the map, it is necessary to synchronize that map externally. (Structural modification of map is to add or delete one or more key value mappings. If we just change the value associated with a key that an instance contains already is not structural modification.) It can be achieved by synchronizing on any object that encapsulate the map. If such object doesn't exist, map should be wrapped with the help of Collections.synchronizedMap() method. The correct time to do this is at the time of creation, in order to prevent unsynchronized access to map. Syntax: where, K is type of keys in the map and V is type of values mapped with it. Next TopicJava-character-issurrogate-method |
in Java The is thrown when we try to convert a string into a numeric value such as float or integer, but the format of the input string is not appropriate or illegal. By illegal format, it is meant that if you are trying to...
3 min read
? In Java, annotations are metadata about the source code. They do not have any direct effect on the execution of the Java program. Annotations in Java were introduced in JDK 5. The main purpose of using annotation is that it gives instructions to the compiler at...
4 min read
Exception handling is a crucial aspect of Java programming, allowing developers to gracefully handle unexpected errors and maintain the stability of their applications. One common exception that Java developers encounter is the InvocationTargetException. In this section, we will explore what InvocationTargetException is, its causes, and how...
5 min read
In programming, we generally need to implement values that can only have one of two values, either true or false. For this purpose, Java provides a special data type, i.e., boolean, which can take the values true or false. The boolean values can be declared with the...
2 min read
Generally, the improvement of the performance of Java applications is a complex process that comprises various tasks and methodologies. Performance optimization thus guarantees that the applications work well, are resourceful and offer a good user experience. Here below is a list of different subjects relevant...
9 min read
Shunting Yard Algorithm in Java The Shunting Yard algorithm is a commonly used algorithm in computer science for converting infix expressions to postfix or prefix expressions. In postfix notation, also known as Reverse Polish Notation (RPN), the operator is placed after the operands, while in prefix notation,...
8 min read
Java provides a number of method calls to check and change the permission of a file, such as changing a read-only file to have permissions to write. File permissions are required to be changed when the user wants to restrict or modify the operations permissible...
5 min read
? In order to write more adaptable, reusable, and type-safe code, developers need to use the Java programming language's generics capability. Generics were initially made available in Java 5 and have since established themselves as a key component in any Java developer's toolbox. In this section, we...
4 min read
(JFR) is a powerful performance monitoring and profiling tool. It diagnoses, collects, and monitors a running Java application on a continuous basis even in catastrophic failure. It is integrated into JVM. The tool provides a powerful GUI for application performance analysis. It is used in...
12 min read
Differences Between Java and JDK Java is a programming language that is highly adaptable and powerful because it can operate on any device that has a Java Virtual Machine (JVM). Developers love it for creating enterprise solutions, mobile apps, and web applications. In order to fully use...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India