Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Using Objects as Keys in HashMap

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get my head around the following subject; if you have a hashmap and use Objects for keys, the results when you change attributes of the Objects seems to be strange. For example



As I understand it, when you try to lookup a value in a hashMap, first the hashCode is computer and then the objects are compared for equality. After we change d1.name we can no longer find the "dog key" because although hashCode is the same (6) the equals will fail (clover!=slover).

So why then does the last line also print null? As far as I can see the hashCode AND the equality test should return true.

Is it because I am trying to find it with a "new" Dog Object and not the actual d1 object? If I were to clone d1 and use the clone instead might that work?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see how you say the equality test is true at the end; the new and old objects have different names.

The short answer is, though, that immutable objects make the best keys, and objects with hashcodes or equality criteria that can change while the key is in the map make completely unsuitable keys -- never use such an object as a key in a Map, period.
 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
I don't see how you say the equality test is true at the end; the new and old objects have different names.



Dog d1 = new Dog("clover");
m.put(d1,"Dog Key");

and

System.out.println(m.get(new Dog("clover")));//also prints null

How exactly do these have different names?

I appreciate that practically I should not be doing this, so let just say this is just theoretically speaking (to help my understanding)
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dave hopkins:

How exactly do these have different names?



Because in between you say

d1.name="slover";
 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I think the confusion has come mainly from mistakes in my coding. I have re-written the above and now hope that it clears everything up. Thanks for your help!

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that there still is a bug in your equals() method:

To compare strings ('name' in this case), you should not use the == operator; you should use the equals() method of class String:
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic