0

I google out the usage of identity hash map but doesn't found a good answer. I also doesn't got the java doc explaination below :

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.

Can some one please provide a good use case of identity hash map ?

Sahil Gupta
  • 2,028
  • 15
  • 22
  • Maybe you want to read this: http://stackoverflow.com/questions/3563847/what-is-the-use-of-hashcode-in-java – GhostCat Aug 23 '16 at 03:32

1 Answers1

0

I guess the important point here is

The node table must not equate distinct objects even if they happen to be equal

If you add a key value pair to a map the e.g. hashmap will check if the key already exists using the equal method. But there are cases where you want to compare the key on its real identify which in Java is the object reference (address). As stated in the Java doc one use case could be a map that manages proxy objects. If you have two objects which are "equal" you still want to create a separate proxy object for both of them. And as for caching you want to store those proxy objects in a map. Then you use the identity map with the source object as key and the proxy object as value.

Hope this makes it a bit clearer.

Rainer
  • 761
  • 5
  • 20