Combining Java hashcodes into a “master” hashcode

后端 未结 2 1822
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 09:19

I have a vector class with hashCode() implemented. It wasn\'t written by me, but uses 2 prime numbers by which to multiply the 2 vector components before XORing them. Here i

2条回答
  •  既然无缘
    2020-12-17 10:22

    That's the normal practice. It looks pretty reasonable to me. If you're using Eclipse, you should find that it can generate equals and hashCode for you—just check the Source menu. It will do the same thing—enumerate your fields and create an equals method that checks all of them, then choose n prime numbers and do what you've done to create a hashCode method.

提交回复
热议问题