This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hashCode/equals and idProperty, references, compositeKeys?

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have three questions about hashCode/equals-Method within an EntityBean:

- Should I integrate a idProperty of an EntityBean into its hashCode/equals-Method?

- Should I integrate all references of my EntityBean into its hashCode/equals-Method?

- Should I integrate the compositeKey of my EntityBean into its hashCode/equals-Method?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the hashCode is used to increase the spead for searching for items in data structures that using hashing as their search mechanisim such as HashSet HashTable and so on ... the hashCode can be effcient and inefficient which will effect on performance only but you will always find your object from the hashed structure as long as you provide an implementation of the hashCode wether its efficient or not.

for example if your hashcode return the value "10" for all instances it will work perfectly however the performance will be very poor. to make it efficient it is better to make the generation of your hashCode as much different as possible from others, but without breaking the hashCode/equals rules.

Should I integrate a idProperty of an EntityBean into its hashCode/equals-Method?

it would be better if you integrate the idProperty since it is the one that differentiates between objects, and if two objects are equal only if they have to identical idProperties then returning the idProperty as your hashCode would be perfect and very efficient.


Should I integrate all references of my EntityBean into its hashCode/equals-Method

No there is no need to integrate all references, it is usually better to include only those members that are used to determine equality of two objects.

Should I integrate the compositeKey of my EntityBean into its hashCode/equals-Method

yes as the idProperty.

hope it helps...

(peace)
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey omar,

thanks that helped me very much!!

Now, I understand the sense of hashCode much more!

Thanks
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!