The IdentityHashMap class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).
Java collections Framework- IdentityHashMap Class
The IdentityHashMap class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words,
in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2).
This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map's general
contract, which mandates the use of the equals method when comparing objects. This
class is designed for use only in the rare cases wherein reference-equality semantics are required.
This class provides all of the optional map operations, and permits null values and the null key. This class makes no guarantees as to the order of the map; in
particular, it does not guarantee that the order will remain constant over time.
This class has one tuning parameter (which affects performance but not semantics):expected maximum size. This parameter is the maximum number of key-value
mappings that the map is expected to hold.
A typical use of this class is topology-preserving object graph transformations,
such as serialization or deep-copying. To perform such a transformation, a program must maintain a "node table" that keeps track of all the object
references that have already been processed. The node table must not equate distinct objects even if they happen to be equal. Another typical use of this class
is to maintain proxy objects. For example, a debugging facility might wish to maintain a proxy object for each object in the program being debugged.
The IdentityHashMap class supports four constructors.
IdentityHashMap( ) - This constructor constructs a new, empty IdentityHashMap with the maximum size (21).
IdentityHashMap(int ExpectedmaxSize) - This constructor constructs a new, empty IdentityHashMap with specified expected maximum size.
IdentityHashMap(Map m) - This constructor constructs a new IdentityHashMap with the same mappings as the specified Map.
The list of methods supported by IdentityHashMap Class are shown given below:
put( ) - Associates the specified value with the specified key in this IdentityHashMap.clear( ) - Removes all mappings from the IdentityHashMap.
isEmpty( ) - Returns true if this IdentityHashMap contains no key-value mappings.
remove( ) - Removes the mapping for this key from this IdentityHashMap if it is present.
keySet( ) - Returns a set view of the keys contained in this map.
entrySet( ) - Returns a collection view of the mappings contained in this map.
values( ) - Returns a collection view of the values contained in this map.
size( ) - Returns the number of key-value mappings in this IdentityHashMap.
Here is the example for demonstration of the IdentityHashMap-
Example:-IdentityHashMapDemo.java
package devmanuals.com; import java.util.*; public class IdentityHashMapDemo { public static void main(String args[]) { IdentityHashMap map = new IdentityHashMap(); // Add the mapping into the map map.put(1, "A"); map.put(2, "A"); map.put(3, "B"); map.put(5, "G"); map.put(4, "N"); System.out.println(" The Map elements are : " + map); } }Output:-
The Map elements are : {1=A, 3=B, 4=N, 5=G, 2=A} |
Download This Code.
Here is the Example of methods implementation of IdentityHashMap class.
[ 0 ] Comments