Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

map object as a key and as a value

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Palash Nandi
Ranch Hand
Posts: 34
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic