*
The moose likes Java in General and the fly likes Identity Hash Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Identity Hash Map" Watch "Identity Hash Map" New topic
Author

Identity Hash Map

Prasana Venkat Ramesh
Greenhorn

Joined: Feb 25, 2013
Posts: 11
Hi, I really don't get what IdentityHashMap is all about. Can someone use this Map object and write a program illustrating it's use? You can also tell me simply what it is in simple terms if writing a program is tough. http://docs.oracle.com/javase/7/docs/api/java/util/IdentityHashMap.html

Thanks in advance,
Prasana
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

IdentityHashMap differs from HashMap in implementation of algorithms of searching element. For example, if you call containsValue method, then simple reference comparing ('==') will be applied instead of equals() method.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

Prasana Venkat Ramesh wrote:Hi, I really don't get what IdentityHashMap is all about.

I presume you've read the API docs. So, what is it that you don't understand?

Can someone use this Map object and write a program illustrating it's use?

No.

You can also tell me simply what it is in simple terms if writing a program is tough.

In the simplest terms I know, it's for storing mappings where the "uniqueness" of each key is based on the object's identity (ie, its reference), rather than equals().

The API docs actually explain it quite well, and even include examples of use (they also say that use cases are fairly rare), so I suggest you read them carefully and come back with a specific question if you don't understand something.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Winston Gutkowski wrote:
The API docs actually explain it quite well

Thanks Winston.
The doc says:
it intentionally violates Map's general contract

What it means? Contract - is an interface, right?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Look at the context of that sentence in the API docs:
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.

The "contract" (what any implementation of interface Map is supposed to do) says that maps should use equals() to compare objects, but IdentityHashMap does not do that. The word "contract" does not refer to any specific Java programming language feature, such as an interface. It's just the specification of what a Map is suppossed to do.

IdentityHashMap is a very specialized kind of map, which you rarely ever need. I've maybe used it two times in my Java programming career since 1998.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

surlac surlacovich wrote:The doc says:
it intentionally violates Map's general contract

What it means?

Well first, you need to read the whole sentence, which says:
"it intentionally violates Map's general contract, which mandates the use of the equals method when comparing objects."
and the easiest place to see that is in the difference between the docs for Map.containsKey() and IdentityHashMap.containsKey(). The latter is only concerned with whether there is already a key with the same reference, so it uses '==' rather than equals().

Contract - is an interface, right?

In this case, yes, java.util.Map is an interface; but you can have contracts mandated by abstract classes as well. In this case, "contract" refers to abstract methods that have to be implemented. Normally, the docs only state what needs to be done to implement them, but in the case of Map.containsKey() it actually includes a formal definition of how it should be implemented as well - and it's that that IdentityHashMap "violates".

HIH

Winston
Prasana Venkat Ramesh
Greenhorn

Joined: Feb 25, 2013
Posts: 11
Jesper de Jong wrote:Look at the context of that sentence in the API docs:
...
IdentityHashMap is a very specialized kind of map, which you rarely ever need. I've maybe used it two times in my Java programming career since 1998.


Hi can I know what context you used it? To do what exactly. The api says it does not use equals() method to prevent some attacks. It's really hard to find that kind of usage for IdentityHashMaps other than security related stuff
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Prasana Venkat Ramesh wrote:Hi can I know what context you used it? To do what exactly. The api says it does not use equals() method to prevent some attacks. It's really hard to find that kind of usage for IdentityHashMaps other than security related stuff

I don't see that part about not using equals() to prevent attacks anywhere in the API documentation. But the API documentation does mention:
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.

I used it once for a collection of objects where I had to store some extra data for each of those objects, but I didn't want to store that extra data in the objects themselves. So I used an IdentityHashMap where the keys were the objects, and the values were the extra data I needed to store for each object. That's a use similar to what's mentioned above about proxy objects.
Prasana Venkat Ramesh
Greenhorn

Joined: Feb 25, 2013
Posts: 11
Thank you Mr Jong.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Identity Hash Map
 
Similar Threads
can anybody tell about map in java
what is the best method to sort strings in real time or off line ?
can anybody tell about map in java
design pattern to avoid lot of if-else condition
substitute for DynaActionForm in struts2