`
hsyzijvaa
  • 浏览: 107197 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

hashcode 重写

阅读更多

    一般hashcode重写都采用了31作为因子

如以下源码
  @Override
  public int hashCode() {
    if (!dirty) {
      return hashCode;
    }
    int result = Arrays.hashCode(pattern);
    result = 31 * result + Longs.hashCode(support);
    result = 31 * result + length;
    hashCode = result;
    return result;
  }


首先31是一个奇数,乘或异或不太重复,分散效果好
其次31有计算优势,31 * i == 32 * i - i == (i << 5) - i.
将乘法转成移位和减法,提高了计算效率。
 
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics