• 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic