Can Java's hashCode produce same value for different strings?

Xara picture Xara · Apr 11, 2012 · Viewed 34.3k times · Source

Is it possible to have same hashcode for different strings using java's hashcode function?or if it is possible then what is the % of its possibility?

Answer

Mat picture Mat · Apr 11, 2012

A Java hash code is 32bits. The number of possible strings it hashes is infinite.

So yes, there will be collisions. The percentage is meaningless - there is an infinite number of items (strings) and a finite number of possible hashes.