It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes map object  as a key and as a value 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "map object  as a key and as a value" Watch "map object  as a key and as a value" New topic
Author

map object as a key and as a value

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

java API

it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value


why map is allowed as a value , but not as a key?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

naveen yadav wrote:hi ranchers ,

java API

it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value


why map is allowed as a value , but not as a key?


Taken out of context. That section in the doc discusses the requirement for keys to be immutable, and how a map adding itself will break since the map is changing. In context, those statements makes sense.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Hi,
It is actually permissible i.e. java will not throw any runtime or compile time exception. BUT its not recommended. Actually the rule applies for any Mutable Key object as pointed out by Henry.
I think every thing will be clear with a small example as follows:



and the output is as follows :



Hope that answers your doubts naveen


A TubeBulb May light slowely... But it lights the Brightest..
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


what i understands is this :

once a map which is used as a key . IT must not be changed



If that is true , then it must be true for both Mutable and Immutable maps ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

naveen yadav wrote:
what i understands is this :


No. Apparently you missed the word "itself" in what you read, since the example you posted is not an example of a map containing itself as a key.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Paul , you are very right. And as obvious from the example , In general we should avoid making a Map a key, because when that key's value changes, it messes everything up.
So if the map contains itself as a key. I guess it will mess up things more :O

So my 2 cents would be in general : Always Avoid Maps as keys (i.e. if you ever intend to change it's value in future )

But i am not very sure about the Immutable Maps question. I am not very clear with the concept. But what is know is that, you cant change it ever :O. So should not be a problem i guess.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I have a hard time understanding why someone would want to use a map as a key anyway. In what situation would that be useful?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: map object as a key and as a value
 
Similar Threads
What kind of Objects can a Hashtable hold?
Confused with Set equality
Why mutable elements of Sets?
Comparing Maps hashcodes problem??
equals() doesn't fail for 2 different strings of the same hashCode Pages 583, 584, 585 of SCJP6