equals() without hashCode()

前端 未结 7 850
自闭症患者
自闭症患者 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 21:59

    Well, obviously You can, but the fact that You don't plan to use hashing is not a sufficient reason not to implement it. Some of the libraries You use could make use of hashing. If you want to avoid testing equals or hashcode, You could try auto-generating those methods (most IDEs have that feature), or use project lombok (https://projectlombok.org)

提交回复
热议问题