• 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

Does Map Support User-Defined Object As Key?

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Map m = new HashMap();
m.put(myobj1, "Hello");
m.put(myobj2, "World");
I'm using my own object as the key for the map.
Since the key is a user-defined object, I would need to overload the equality operator in order to get the value.
Now, if I would to do something like this:
MyObj myObj = (MyObj)m.get(myobj3);
I found out that the equality operator for MyObj never even executes (by printing out some message). But if I would to retrieve using the original myobj1 and myobj2 object, then only the equality operator would get executed.
If I would to do the above using the String object instead of MyObj, the equality operator works fine.
I can't seemed to find a reason for the above behaviour. Any ideas?

Thanks in advance.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you override equals() you must also override hashCode(). Did you?
If you did override hashCode(), did you ensure that equal objects have equal hash codes?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's happening is, when you use the get(key) method of HashMap, then HashMap calls the hashCode() method of the key first, to see approximately where in the HashMap the corresponding entry should be located. (I.e., in which "bucket".) If it finds one or more existing entries in the appropriate bucket, it will then call the equals() method to determine which (if any) of the entries really has the desired key. Thus, whenever you override equals() you should also override hashCode() to be consistent with the new equals() definition - and vice versa. Because if two objects have different hashCode() values, the HashMap will probably never even bother to check the value of equals(). It is assumed that the two objects with different hash codes are unequal - as per the hashCode() API.
You can read more about this in the Object API, or in Sun's Java Tutorial here. Joshua Bloch's book Effective Java has an excellent discussion of how to write good hashCode() and equals() methods (as well as numerous other helpful topics). For that matter, you can find many past discussions here, such as, say, this one.
 
James Gordon
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
... think I know how to proceed from here.
Thanks for all the comments.
I never realized that we need to overload the hashCode() as well!
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an excellent article in the JavaRanch Newsletter this month that explains this very issue. You can find it here. It is a must read for people that were unaware of this hashCode/equals relationship.
Jamie
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic