How to implement `equals` and `hashCode` on `List` subtypes?

I also stumbled over this and I have to disagree with Wasabi375.

It’s true, Javas List interface does not actually implement equals (which would be impossible), but it overwrites the JavaDoc to clearly state, what a List.equals method must fullfil. It must compare elements by index etc. This is much more specific then just relying on the general contract of Object.equals (or in Kotlin Any.equals).

So following this, in Java, I can be sure that any lists are equal if they have equal elements. Since Kotlin wants to be compatible (and in practice it is) the KDocs of List.equals and Set.equals etc. should also be aligned to Java.

2 Likes