不同字符串的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