How should I map long to int in hashCode()?

后端 未结 6 1433
囚心锁ツ
囚心锁ツ 2020-12-25 11:10

I have a range of objects that have a long field whose value uniquely identifies a particular object across my entire system, much like a GUID. I have overriden

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 11:31

    int result = (int)((longVal >> 32) ^ longVal);
    

    will be more well distributed, because modulo will not return different value if only upper bits of your long value has changed.

提交回复
热议问题