javahashcode

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


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?


Solution

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