不同字符串的hashcode可能相同


System.out.println("重地".hashCode()=="通话".hashCode());//结果为true

hashcode()方法的返回值是int类型,当计算出的hashcode超过了int的范围,hashcode的准确性就不能保证了,所以不同字符的hashcode是有可能相同的。

public static int hashCode(byte[] value) {
  int h = 0;
  for (byte v : value) {
    h = 31 * h + (v & 0xff);
  }
  return h;
}
public static int hashCode(byte[] value) {
  int h = 0;
  int length = value.length >> 1;
  for (int i = 0; i < length; i++) {
    h = 31 * h + getChar(value, i);
  }
  return h;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM