• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

order of execution of hashCode() and equals(Object) in Map

 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it correct that when u put certain object in a Map then no
.equals(Object) is called. Only hashcode() is called.

When u call get(), then order is first hashCode(), then equals(Object).

Thanks

Naseem Khan
 
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
Both put() and get() may need to call equals(); it depends on the contents of the Map when these methods are called. But they're free to call the methods however they like -- there's nothing about the number or order of calls in any specification.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in K&B its mentioned that in Map when u get object frm Map, first it looks for hashCode() which will give the right bucket, then it look for the right object in that bucket. For that it calls equals(Object). I have jst one object in that bucket but still it calls equals(Object).

It means for get(), order is hashCode first and then equals(Object). ALWAYS

am I right?

regards

Naseem
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naseem, you are right. "The order is hashCode first and then equals(Object). ALWAYS". For example, search google for "HashMap.java".
 
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
If the key is not in the HashMap, and no other keys in the Map hash to the same bucket, then equals() won't be called -- so even given Sun's current HashMap implementation, your "ALWAYS" isn't really "ALWAYS." But the point I'm trying to make is that there's no official specification or rule that says HashMap will call this method, then this method, in any specific order. It's fine to try to understand how things work, but reducing things to oversimplified rules this way is just silly.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Ernest. There is the exception (key == null) in the source code.
 
Do you pee on your compost? Does this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic