equals() without hashCode()

前端 未结 7 848
自闭症患者
自闭症患者 2020-12-20 21:17

Can I only implement equals() but not hashCode() if I only need to compare objects and not yet plan to put the objects into any hash based containers?

Seems all Jav

7条回答
  •  自闭症患者
    2020-12-20 22:13

    Oracle's tutorial answers this

    The hashCode() Method

    By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid. Therefore, if you override the equals() method, you must also override the hashCode() method as well.

提交回复
热议问题