• 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 in Hashcode()

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In khalid Mughal page 511, hashcode() method is given in detail. I have nor understood certain questions:

What will happen if unequal objects will hash to same bucket ? (page 513)
I know that equal objects should hash to same bucket in the hash table, it enables yielding correct answer on searching that object. But where is the prob if unequal objects will hash to same buckets??? Does it mean that in a bucket only equal objects should be placed???
Assuming ,to deal with hash collision a linked lists is maintained at every array index.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well unequal objects can land in the same hash bucket. there is no problem in that. The problem arises when equal objects land in different hash buckets. This happens if a class overrides the equals method but not the hashCode method...
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

carox kaur wrote:But where is the prob if unequal objects will hash to same buckets???


The only "problem" is performance. A hash map / set would have to look through more items to find the truly equal object. Other than that, no problem. In fact, it is perfectly legal to return a constant (like 42) as the hash code - but then your hash map / set turns into a linked list instead.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic