• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

which hashCode() is faster ??????

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While goiing through the K&B certifivcation book, I found the following code and lines:
FIRST : public int hashCode() {return name.length(); }
SECOND: public int hashCode() {return 4; }

It was written in the book that SECOND hashcode method is slower than the FIRST hascode.
What does it means of saying faster(or slower) hashcode in the present context??
Please explain if anyone having clear idea of this.
Thanks in advance.
--------------------------------------------------------
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It obviously does not mean the computation of the hashcode because in this example the first computation needs more effort than the second.

What it means is - if you use objects with these hashfunctions in a searchable (hash based) collection, then it will take longer in the second case to find the object within the collection because all the objects would be in the same hash bucket. (Unless the names are all of the same length, of course).
[ January 30, 2007: Message edited by: Barry Gaunt ]
reply
    Bookmark Topic Watch Topic
  • New Topic