• 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

S&B hashCode() clarification needed.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 563 of SCJP 6 by Sierra and Bates the following:

When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they don't override hashCode(), the default Object. hashCode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.



Since equals() not hashCode() determines what is a duplicate in HashSet I don't understand the above. Even if two objects o1.hashCode() == o2.hashCode() is true but o1.equals(o2) is false both o1 and o2 will be added to the Set. The above quote seems to indicate that if the hashCode is equal determines what is considered to be a duplicate object... or maybe I'm not reading it correctly.


.hashCode() only determines the efficiency of Set operations like add and remove... and hashCode() should never be used to determine uniqueness.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Elliott wrote:On page 563 of SCJP 6 by Sierra and Bates the following:

When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they don't override hashCode(), the default Object. hashCode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.



Since equals() not hashCode() determines what is a duplicate in HashSet I don't understand the above. Even if two objects o1.hashCode() == o2.hashCode() is true but o1.equals(o2) is false both o1 and o2 will be added to the Set. The above quote seems to indicate that if the hashCode is equal determines what is considered to be a duplicate object... or maybe I'm not reading it correctly.


.hashCode() only determines the efficiency of Set operations like add and remove... and hashCode() should never be used to determine uniqueness.



No, that's not what they're saying. They're saying that even if o1.equals(o2), both will be allowed in the set unless you override hashCode.

To see this, run the following code. All Equalizers equal all other Equalizers -- in fact, all other objects -- so the set should contain only one element, correct?
 
Ranch Hand
Posts: 63
Mac OS X jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steven,

Pay attention to what Sierra says about buckets. hashCode() plays a very important role in storing Set's values. It's like an index, but it's better you think about boxes. hashCode() tells you which box you have put your stuffs. Inside the box, you find your stuffs and using equals() method, you find the exactly thing you was looking for.

Edit: Look into page 551. There she explain exactly the process.

--eduardo
 
Steven Elliott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No, that's not what they're saying. They're saying that even if o1.equals(o2), both will be allowed in the set unless you override hashCode.



I guess what I was looking for was BOTH o1.equals(o2) AND o1.hashCode() == o2.hashCode() need to be true otherwise the two objects will be inserted into the Set. I just didn't read it that way in the explanation.

The JavaDoc actually says for add(e)

Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)).



Apparently it assumes the contract for equals such that if o1.equals(o2) then o1.hashCode() == o2.hashCode() is true.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic