equals()를 재정의하지만 hashCode()는 재정의하지 않는 클래스 또는 그 반대의 클래스를 보고합니다.
또한 equals() 또는 hashCode()를 재정의하는 객체 선언을 보고합니다.
이러한 항목은 클래스가 Collection에 추가될 때 바람직하지 않은 동작을 발생시킬 수 있습니다.
예:
class C1 {
override fun equals(other: Any?) = true
}
class C2 {
override fun hashCode() = 0
}
object O1 {
override fun equals(other: Any?) = true
}
object O2 {
override fun hashCode() = 0
}
빠른 수정에서는 클래스의 equals() 또는 hashCode()를 재정의하고 객체의 해당 메서드를 삭제합니다.
class C1 {
override fun equals(other: Any?) = true
override fun hashCode(): Int {
return javaClass.hashCode()
}
}
class C2 {
override fun hashCode() = 0
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}
}
object O1 {
}
object O2 {
}