• 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:

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: 22849
132
Eclipse IDE Spring 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic