equals and hashCode: Is Objects.hash method broken?

后端 未结 4 1807
有刺的猬
有刺的猬 2021-01-03 04:51

I am using Java 7, and I have the following class below. I implemented equals and hashCode correctly, but the problem is that equals r

4条回答
  •  生来不讨喜
    2021-01-03 05:15

    it's not necessary for two unequal objects to have different hashes, the important thing is to have the same hash for two equal objects.

    I can implement hashCode() like this :

    public int hashCode() {
        return 5;
    }
    

    and it will stay correct (but inefficient).

提交回复
热议问题