Back to Intellij Community

KotlinArrayHashCode

plugins/kotlin/code-insight/descriptions/resources-en/inspectionDescriptions/KotlinArrayHashCode.html

2025.3-rc-2554 B
Original Source

Calling hashCode() on an array returns the identity hash code, not a hash based on the array's contents. This is almost never what you want.

To calculate the hash code for an array correctly, use:

  • contentHashCode() for linear arrays
  • contentDeepHashCode() for multidimensional arrays

Example:

fun main() {
      val a1 = arrayOf<Any>()
      val hashcode = a1.hashCode() // incorrect
  }

After the quick-fix is applied:

fun main() {
      val a1 = arrayOf<Any>()
      val hashcode = a1.contentHashCode() // correct
  }