IdentityHashMap Class in JavaThe 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 BF |
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