• Post Reply Bookmark Topic Watch Topic
  • New Topic

if HashCode() gets called before equals()?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I noticed that when adding to a HashSet the classes HashCode method gets called first,so if the hashCode method gets called first,how does the equals method get called at all the reason I am saying this is because lets say we add Body("Pluto") Pluto and pretend Pluto is already added to the set these two objects will have the same hashcodes so then the equals method will be called here it first checks if(this == obj) pluto does == pluto the hashcodes are the same so it will return true without ever getting to compare if their strings are equal??









but when I run the code,this run gets printed twice so obviously it gets to comparing the strings in my equals() method?
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this == obj
Only compares the references and has nothing to do with hash codes or equals(). Two bodies can both be named Pluto but each will have its own reference.

Hash code is called first because it is usually cheaper than calling equals(), however two different objects can generate the same hash code so after the hash codes are determined to be equal then the equals() method needs to be called to see if the values contained in the object are equal.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The part about equals() being called because two different objects can have the same hash code is true. However, hashCode() is not called first because it's cheaper, but because equals() can not be used to determine in what bucket of a hash table an object might reside. hashCode() is first called to determine the bucket the object might be in, and then equals() is called to determine if any of the objects inside the bucket is the same object.

For a more detailed explanation of hash tables, read the Wikipedia article on Hash table. It's quite technical though, and it's not necessary to know by heart before you can use HashMap and HashSet effectively.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys for all the info,much appreciated =)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what you might do well to know, but you probably don't really need to know it:-
  • 1: A HashMap has a backing array containing Map.Entrys, defaulting to size 16 and doubling every time it is full.
  • 2: If h means hash code and c means capacity (= size of array) you use this formula: h & c - 1
  • 3: That formula finds the remainder (sort of) after dividing by c, but it is always positive. It only works correctly if c is an exact power of 2.
  • 4: That formula is spot-on for finding the index in an array to locate the Key. Only if the size of the array is an exact power of 2.
  • 5: The locations in the array are called buckets.
  • 6: If you have several keys in the same bucket, which is called a collision, you can use their hash codes to distinguish them. h₁ ≠ h₂ ⇒ ¬equals
  • The last bit means that if the two hashcodes are different, you already know the equals method is going to return false, so you don't even try equals. It is only worth trying equals if you find two hash codes the same.
    You use similar techniques for inserting things and for searching.
    You can remind yourself of the details of hashCode() here; equals means the two hash codes are the same, so different hash codes mean equals must return false. If you know you are going to get false anyway, who needs to call equals()?

    Another thing you need to know about hash‑based data structures. If the hash code of the element changes, it may become impossible ever to find it again. (That only applies to the key in a hash map.)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!