I'm working with a collection of data class instan...
# getting-started
j
I'm working with a collection of data class instances and some of them might share the exact same data. I'd like to put these as keys in a map, but I want each instance to be its own unique key. Is there a way to do this? Can I create my own subclass of map which uses `Any`'s implementation of
equals
and
hashcode
for the keys even if they are data classes?
I figured out a way to create a simple wrapper class to do this. If anyone is interested:
Copy code
class IdentityMap<K, out V> private constructor(
    delegateMap: java.util.TreeMap<K, V>
) : Map<K, V> by delegateMap {
    constructor(
        data: Collection<Pair<K, V>>
    ) : this(
        java.util.TreeMap<K, V>(compareBy(System::identityHashCode)).apply { putAll(data) }
    )
}
j
There's always one more class I don't know about, it seems.
Thanks @ephemient