by alexhutcheson · 5y ago · 0 comments
For mutable objects, the notion of equality is hairy in general, because what you want often depends on why you'd be comparing the two objects. You might have two mutable objects that can be meaningfully compared in your business logic, but absolutely should not be used as keys in a map. Often you can satisfy your goals by implementing a separate method that does not attempt to conform to the 'equals' contract, which ensures it will not unintentionally be used for e.g. data structure ordering.
[1] https://github.com/google/auto/blob/master/value/userguide/i...
[2] https://projectlombok.org/features/EqualsAndHashCode
[3] https://blogs.oracle.com/javamagazine/records-come-to-java