• 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

doubt regarding efficiency of hashcode

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In kathy sirrae and bertbates scjp 1.6,page no:585.there are two hashCode snippets as follows.

in the above 2 snippets he gave first one is efficent(fast)
reason:the first hashCode generate different code for each name length.
the second hashcode forumla woill always produce same result 4 so it will be slower than the first

my understanding of above reason is if the fist code is used the objects will stored in diff bugets so while retrieving it is easy.
in the second code every object will generate same hashCode so all objects will be stored in the same bucket,while retrieving it will take
time.so fist is better. am i right??
please some body explain me if i am wrong.


 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:my understanding of above reason is if the fist code is used the objects will stored in diff bugets so while retrieving it is easy.in the second code every object will generate same hashCode so all objects will be stored in the same bucket,while retrieving it will take time.so fist is better. am i right??



Yes. When you add to the same bucket, there will be a list of objects in it. So to retreive an object list has to be searched which is why that's not an efficient way to generate a hashcode.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:Hi all,
In kathy sirrae and bertbates scjp 1.6,page no:585.there are two hashCode snippets as follows.

in the above 2 snippets he gave first one is efficent(fast)
reason:the first hashCode generate different code for each name length.
the second hashcode forumla woill always produce same result 4 so it will be slower than the first

my understanding of above reason is if the fist code is used the objects will stored in diff bugets so while retrieving it is easy.
in the second code every object will generate same hashCode so all objects will be stored in the same bucket,while retrieving it will take
time.so fist is better. am i right??
please some body explain me if i am wrong.


Right. With the second hashCode method all Strings will be saved in the same bucket. If you want to retrieve a string out of a String collection,
the time it needs depends on the size of the bucket, which is here the size of the collection.

cheers
Bob
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vijitha and Bob
 
reply
    Bookmark Topic Watch Topic
  • New Topic