By default which techniques is used by JAVA to handle hashcode collision ? Is it separate chaining or does it depend on the JVM implementation for different platform?
Asked
Active
Viewed 170 times
-2
-
'*Why* default'? Do you mean '*by* default'? If not, what *do* you mean? Which of the numerous hashing classes are you referring to? – user207421 Jul 17 '13 at 10:21
-
http://stackoverflow.com/questions/4980757/how-do-hashtables-deal-with-collisions – Salih Erikci Jul 17 '13 at 10:25
-
Sorry It was typing mistake, It is by default. Since there are numerous strategies to handle hashcode collisions like linear probling, separate chaining, double hashing. So which technique is JAVA using using by default to handle hashcode collisions? – Rahul Jul 17 '13 at 10:25
-
1The source is available, you know. – Dave Newton Jul 17 '13 at 10:27
-
@Rahul: there are hundreds of classes overriding hashCode(). Which one are you concerned about? – JB Nizet Jul 17 '13 at 10:30
-
You seriously mean you magically made the same 'typing mistake' twice? once in the title and again in the question? without noticing? Don't you think you should present your questions more carefully? And have you considered answering my *other* questions? What makes you think there even *is* a 'default'? As it stands your question is meaningless. – user207421 Jul 17 '13 at 10:30
-
@EJP: the magical formula is Ctrl-V. – JB Nizet Jul 17 '13 at 10:31
-
@JB Nizet I am talking about Object class – Rahul Jul 17 '13 at 10:32
1 Answers
0
As explained by the javadoc:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
So, if the JVM implements hashCode() using this strategy, unless you have a whole lot of heap space and are extremely unlucky, you won't have any collision.
That said, the Object's hashCode()
is not very important in practice, because most of the classes used as keys in HashMaps override the hashCode()
method.

JB Nizet
- 678,734
- 91
- 1,224
- 1,255
-
Thanks JB. Consider a scenario in which I am making a class ABC and overriding hashCode method which returns constant value '11'. I am creating three objects of ABC class which are having same hashcode as hashCode method returns same value as implemented. So I want to ask how would JAVA have stored these objects in bucket, using separate chaining or linear probing? Am I clear? – Rahul Jul 17 '13 at 10:40
-
@Rahul They will store the key as well as the value in the bucket , if `hashCodes()` collide then use `equals()` ! Hence a sound definition of `equals()` is needed ! – AllTooSir Jul 17 '13 at 10:41
-
Look at the HashMap source code. It uses a chained list of Entry instances containing the keys. – JB Nizet Jul 17 '13 at 10:41