equals() without hashCode()

前端 未结 7 851
自闭症患者
自闭症患者 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:24

    Can I only implement equals() but not hashCode()?

    Yes You can . Because they are just to method from the parent class Object, So it actually your choice to implement or not to (together or individual.)

    All Java bibles say these two MUST be implemented together.

    If you are not using anything related to hashcode(as you said hash based container) it not a MUST, But it is a good practice to implement them together to avoid any unexpected circumstances.

提交回复
热议问题