Yes, java has Objects.hash(...), but I’ve always hated that it allocates a new array and boxes all the parameters. There should really be a simple Kotlin alternative that doesn’t have all that overhead.
If you have fields a, b, c, d, and e, what’s the best way to combine their hashes in kotlin?
At this moment I don’t think that there is any better solution than yours Although a potential approach would be to use an inline function:
inline fun hashCodeOf(a: Any?, b: Any?, c: Any?): Int = a.hashCode()
.times(31).plus(b.hashCode())
.times(31).plus(c.hashCode())
private class MyDataClass(val a: Int, val b: Long, val c: Double) {
override fun hashCode() = hashCodeOf(a, b, c)
override fun equals(other: Any?) = this === other || other is MyDataClass
&& a == other.a
&& b == other.b
&& c.compareTo(other.c) == 0
}
Unfortunatelly, even after inlining a boxing issue still remains. This could probably be optimized if an official ticket was posted
Forgive me if I’m pointing out the obvious, but are you aware that a kotlin data class creates equals, hashCode, and toString for you?
Also, if I’m creating a hashcode function for something other than a data class that needs it, I usually go for speed (and less prone to manual mistakes) over beauty, so I personally prefer alt + insert generate “equals() and hashCode()”