I faced a similar kind of issue while using a custom object as a key in Treemap. Whenever you are using a custom object as a key in hashmap then you override two function equals and hashcode, However if you are using ContainsKey method of Treemap on this object then you need to override CompareTo method as well otherwise you will be getting this error Someone using a custom object as a key in hashmap in kotlin should do like following
data class CustomObjectKey(var key1:String = "" , var
key2:String = ""):Comparable<CustomObjectKey?>
{
override fun compareTo(other: CustomObjectKey?): Int {
if(other == null)
return -1
// suppose you want to do comparison based on key 1
return this.key1.compareTo((other)key1)
}
override fun equals(other: Any?): Boolean {
if(other == null)
return false
return this.key1 == (other as CustomObjectKey).key1
}
override fun hashCode(): Int {
return this.key1.hashCode()
}
}