In Java, why must equals() and hashCode() be consistent?

后端 未结 6 1158
挽巷
挽巷 2020-11-28 13:13

If I override either method on a class, it must make sure that if A.equals(B) == true then A.hashCode() == B.hashCode must also be true.

Can

6条回答
  •  误落风尘
    2020-11-28 13:41

    The idea behind this is that two objects are "equal" if all of their fields have equal values. If all of fields have equal values, the two objects should have the same hash value.

提交回复
热议问题