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

前端 未结 12 880
情歌与酒
情歌与酒 2020-11-28 08:58

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?

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 09:50

    Yes, it is possible for two Strings to have the same hashcode - If you take a look at the Wikipedia article, you will see that both "FB" and "Ea" have the same hashcode. There is nothing in the method contract saying a hashCode() should be used to compare for equality, you want to use equals() for that.

    Since Java 1.2, String implements hashCode() by using a product sum algorithm over the entire text of the string.

提交回复
热议问题