hashCode on Array

ID

kotlin.hashcode_on_array

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

array, reliability

Description

Reports calls to hashCode() on an array type.

Arrays in Kotlin (and Java) inherit hashCode() from Object, which returns an identity-based hash derived from the object’s memory address. Two arrays with identical elements will produce different hash codes, making hashCode() on arrays useless for content-based equality or use as map keys.

Rationale

val a = intArrayOf(1, 2, 3)
val b = intArrayOf(1, 2, 3)

a.hashCode() == b.hashCode()   // FLAW — false: identity hashes differ

a.contentHashCode() == b.contentHashCode()   // OK — true: content matches

Remediation

Replace hashCode() on arrays with the appropriate content-aware alternative:

  • Use contentHashCode() for single-dimensional arrays.

  • Use contentDeepHashCode() for nested (multi-dimensional) arrays.