• 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

why we can't avoid hash collision in practice ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why we can't avoid hash collision in practice ? Hashmap use array of array to store value, the reason is hash key value collision. Then why we can't avoid it ? for me, I didn't see how difficult it is, we just use a primary key like in the db ...

Thanks.
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also want to know.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, sometimes you can avoid it, and sometimes you can't. What if the key is a String? How many different possible Strings are there that might go into a Map? Hint: it's a really, really big number. The hashCode() must be an int however. How many different possible ints are there? Hint: Integer.MAX_VALUE is a big number, but much much smaller than the previous big number. Therefore, out of all possible Strings, some Strings must have the same hashCode() - no matter how you choose to calculate hashCode(). This leads to the possibility of hash collisions.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Chen wrote:Hashmap use array of array to store value


no, it uses Array.
 
Thakur Sachin Singh
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is good information for us...but i want some more information about this.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike already explained the reason.

Let's have a look at String objects that consist of exactly 10 letters of the alphabet (A-Z). How many different strings are possible? The answer is: 26^10 = 141.167.095.653.376 different strings.

The hash code of an object is a 32-bit integer. So, how many different hash codes are possible? 2^32 = 4.294.967.296 different hash codes.

You see that there are many more possible strings than there are hash codes. Therefore, there must be different strings that have the same hash code.
 
Thakur Sachin Singh
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the information..its valuable for me
 
reply
    Bookmark Topic Watch Topic
  • New Topic