A library for implementing equals and hashCode
Liam Miller-Cushon
cushon at google.com
Tue Apr 23 22:39:48 UTC 2019
[received off-list]
There's some related discussion under "What’s the relationship to
Comparator?". There are at least four options: Equivalence extends
Comparator, Comparator extends Equivalence, there's no relationship between
them, or they both extend some new common super type. There are trade-offs
here, but none of those options seem like a slam-dunk.
The handling of subtypes also affects the choice between instanceof and
getClass() (see "Does equals use instanceof or getClass()?"), but we may
have found a way to side-step part of that debate.
On Mon, Apr 22, 2019 at 12:20 PM Fred Toussi <fredt at users.sourceforge.net>
wrote:
> Since 2014 HSQLDB has been using an ObjectComparator that extends
> Comparator for its hash sets and maps. These are sets and maps for
> combinations of int, long and Object, as well as order preserving sets and
> maps.
>
>
> https://sourceforge.net/p/hsqldb/svn/HEAD/tree/base/trunk/src/org/hsqldb/lib/ObjectComparator.java
>
> https://sourceforge.net/p/hsqldb/svn/HEAD/tree/base/trunk/src/org/hsqldb/map/BaseHashMap.java
>
> We made a shortcut to extend Comparator, but if this is going to be added
> to Java, your Equivalence<T> should be the super interface of Comparator<T>
>
> You may also consider the problems of correctly implementing equals in
> subclasses, which took years to be clarified (by Martin Odersky AFAIR) by
> calling super.equals(other) before performing the test. Example below from
> HSQLDB code.
>
> public class RowType extends Type {
> public boolean equals(Object other) {
>
> if (other == this) {
> return true;
> }
>
> if (other instanceof RowType) {
> if (super.equals(other)) {
> ....
>
> Regards
>
> Fred Toussi
> HSQLDB Project
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20190423/4dcaa4f1/attachment-0001.html>
More information about the amber-spec-experts
mailing list