• 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

Actually when hashCode() is called ?

 
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir I am really confused , when hashCode() is called ? if we are overriding hashCode, at the time of constructor calling it is called, but how ? And for being two object equal their hashCode value must be same , and equals should return true, but if we are overriding hashCode only not equals then .... when we add those two object to a hashSet collection, hashCode() is called and then equals will be called but actually we didn't override equals in our class so Object's equals will be called which again checks for hashCode which is same , but both are added to a not duplicable collection. Please help me
 
Bartender
Posts: 5469
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Pramod,

well, the size of the HashSet is 2, and indeed, that's because you didn't
override the 'equals' method. And although the hashCode() gives the same
hashcode (name.length), t1 and t2 are considered to be unequal. Therefore,
both t1 and t2 are incorporated in the hashset.

If you remove the comments from the 'equals' method, then the size of the hashset
will be 1, since t1 and t2 are now considered to be equal.

Hope this answers your questions.

Greetz,
Piet

PS: hashCode() is called when you put an object in a datastructure that uses
hashcodes, like HashSet and HashMap,
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod Kumar Pandey wrote:Actually when hashCode() is called ?



Well, it is an implementation detail of the hashing collections... but generally, after a hashing is done to determine the bucket, the equals() method is called to compare it to other elements in the same bucket. So, if there is not other element in the bucket, then the equals() method may not be called at all.

Pramod Kumar Pandey wrote:Sir I am really confused , when hashCode() is called ? if we are overriding hashCode, at the time of constructor calling it is called, but how ? And for being two object equal their hashCode value must be same , and equals should return true, but if we are overriding hashCode only not equals then .... when we add those two object to a hashSet collection, hashCode() is called and then equals will be called but actually we didn't override equals in our class so Object's equals will be called which again checks for hashCode which is same , but both are added to a not duplicable collection. Please help me



The Object class equals() method does not compare hash codes. It just checks to confirm that it is the exact same object (same as using the == comparison operator).

Henry
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Pramod Kumar Pandey wrote:Actually when hashCode() is called ?



Well, it is an implementation detail of the hashing collections... but generally, after a hashing is done to determine the bucket, the equals() method is called to compare it to other elements in the same bucket. So, if there is not other element in the bucket, then the equals() method may not be called at all.

Pramod Kumar Pandey wrote:Sir I am really confused , when hashCode() is called ? if we are overriding hashCode, at the time of constructor calling it is called, but how ? And for being two object equal their hashCode value must be same , and equals should return true, but if we are overriding hashCode only not equals then .... when we add those two object to a hashSet collection, hashCode() is called and then equals will be called but actually we didn't override equals in our class so Object's equals will be called which again checks for hashCode which is same , but both are added to a not duplicable collection. Please help me



The Object class equals() method does not compare hash codes. It just checks to confirm that it is the exact same object (same as using the == comparison operator).

Henry



Sir Henry till now I just read that by default the Object's equals() uses == which means checking hashCode(also called deep comparison) ? then if two object having same code should be meaningfully equivalent.
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod Kumar Pandey wrote:

Henry Wong wrote:

Pramod Kumar Pandey wrote:Actually when hashCode() is called ?



Well, it is an implementation detail of the hashing collections... but generally, after a hashing is done to determine the bucket, the equals() method is called to compare it to other elements in the same bucket. So, if there is not other element in the bucket, then the equals() method may not be called at all.

Pramod Kumar Pandey wrote:Sir I am really confused , when hashCode() is called ? if we are overriding hashCode, at the time of constructor calling it is called, but how ? And for being two object equal their hashCode value must be same , and equals should return true, but if we are overriding hashCode only not equals then .... when we add those two object to a hashSet collection, hashCode() is called and then equals will be called but actually we didn't override equals in our class so Object's equals will be called which again checks for hashCode which is same , but both are added to a not duplicable collection. Please help me



The Object class equals() method does not compare hash codes. It just checks to confirm that it is the exact same object (same as using the == comparison operator).

Henry



Sir Henry till now I just read that by default the Object's equals() uses == which means checking hashCode(also called deep comparison) ? then if two object having same code should be meaningfully equivalent.


I surfed on net, and I got your point. Thank you so much SIR. One more question -> why ArrayList or other classes doesn't use hashing , because our sole purpose is to improve performance of the collection.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod Kumar Pandey wrote:
I surfed on net, and I got your point. Thank you so much SIR. One more question -> why ArrayList or other classes doesn't use hashing , because our sole purpose is to improve performance of the collection.



Not all algorithms will work in all cases. For example, lists are ordered -- can you think of a way to do hashing *and* keep ordering?

Also, not all algorithms are best for everything. This is why there is an array list and a linked list.

Henry
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Pramod Kumar Pandey wrote:
I surfed on net, and I got your point. Thank you so much SIR. One more question -> why ArrayList or other classes doesn't use hashing , because our sole purpose is to improve performance of the collection.



Not all algorithms will work in all cases. For example, lists are ordered -- can you think of a way to do hashing *and* keep ordering?

Also, not all algorithms are best for everything. This is why there is an array list and a linked list.

Henry


Thanks Sir !
 
reply
    Bookmark Topic Watch Topic
  • New Topic