• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap keys

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I use an object as a key value, is a reference to the object stored in the Map or is an entire new object created and a reference to this new object store in the Map?

Consider the foll.

1. d is a ref. to a Dog object having one String member variable.

2. I use the above reference as a key in a Map.

3. I now change the value of the above mentioned String variable in such a way that the hashcode also changes (assume that the length of the string variable is the hashcode).

4. Does the position of the object in the Map change as the hashcode has changed? Is the value corresp. to the key now placed in a different 'bucket'?

I would really appreciate if someone could clear my doubts.

Regards
Percy
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Percy,



When I use an object as a key value, is a reference to the object stored in the Map or is an entire new object created and a reference to this new object store in the Map?




It is only reference of the object that is stored in the Map. And if you make change to the object the inserted reference is referring to, it will affect that too obviously.

In case you create a new object and assign it to the reference variable, it wont affect the reference of object the map is having ofcourse.


Thanks,
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Percy]: When I use an object as a key value, is a reference to the object stored in the Map

Yes.

or is an entire new object created and a reference to this new object store in the Map?

No.

Consider the foll.

1. d is a ref. to a Dog object having one String member variable.

2. I use the above reference as a key in a Map.

3. I now change the value of the above mentioned String variable in such a way that the hashcode also changes (assume that the length of the string variable is the hashcode).

4. Does the position of the object in the Map change as the hashcode has changed? Is the value corresp. to the key now placed in a different 'bucket'?


Whenever you change the value of a key after you've already inserted the key into a HashMap (or any Map, probably) you may get unpredictable results which can be summarized as "it doesn't work". The key is still in the map at the old location, but you probably won't be able to find it any more. Basically, you never want to change the value of a key after putting it in a Map.
[ May 07, 2007: Message edited by: Jim Yingst ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:

{Mack=12}
{JillyJill=14, JillyJill=15}



Thanks,
[ May 07, 2007: Message edited by: Chandra Bhatt ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
...{JillyJill=14, JillyJill=15}...


The important thing to note is that the map is "broken." It still contains a pair "JillyJill=14," but the value can no longer be retreived using the key (p1) because p1's hashCode has changed.
reply
    Bookmark Topic Watch Topic
  • New Topic