I just noticed that, in contrast to what is done in Java, the
List interface in Kotlin does not define either
This is also true for other interfaces like
From what I understand, this means that the following statements are not necessarily
true (although they actually are in practice):
listOf(1) == arrayListOf(1)
listOf(1).hashCode == arrayListOf(1).hashCode
So, this leads to the following questions:
- Why doesn’t the
Listinterface in Kotlin explicitly override
hashCode(as its counterpart in Java does)?
- Say I want to create a custom
MyEmptyList. Instances from this class are always empty. This means that
MyEmptyList() == emptyList()should be always
true. How should
hashCodebe implemented in this case?