Search code examples
kotlindata-class

How to hash data class with array property


I have data class ItemMainData(val title: String?, val fields: Array<Field>). What's the proper way to override hashCode()?

I am aware of this answer (Equals method for data class in kotlin), but in that case the hash of the array is sufficient to be the hash of the data class. It's not so in my case


Solution

  • If you press "Generate" in the IDE and select "equals() and hashCode()" from the menu, it will generate the correct implementation for you. The implementation will use the Arrays.hashCode() method to generate the hash code of the array.