androidkotlinhashmap

Unable to remove a HashMap entry by key object even when the key is confirmed to exist using Java HashMap in Kotlin


I need help understanding why I can't remove an entry from a hashmap by an data class object key.

The expectation is that the incoming entry is a key that exists in the map. All markups are immutable data class objects. Sometimes this is failing so in order to understand and trap this cause, I put a series of runtime asserts to check the problem.

val markupFeatureCollection = HashMap<Markup, Feature>()

fun remove(entry: Markup) {
    // the entry is guaranteed to be a valid key for an entry
    val feature = markupFeatureCollection.remove(entry)

    // oh no, this shouldn't happen. We want to re-associate a markup with it's feature. In this special case
    // the markup changed visibility but the feature representation of it should be preserved so it can be
    // shown again without having to rebuild it.
    if (feature == null) {
        // actually look for the markup key in the collection by id
        val member = markupFeatureCollection.filter { it.key.uuid == entry.uuid }

        // assert that there's a feature for this markup
        member.values.first()

        // get the key
        val markup = member.keys.first()

        // sanity test, does the markup we've failed to look up match the one we found with the same id
        assert(markup == entry) {
            "entry key was found ing markupFeatureCollection but could not be removed"
        }

        assert(markup === entry) {
            "entry key was found ing markupFeatureCollection but could not be removed"
        }

        // sanity test, log both side-by-side to compare the values
        println("xxx: $markup")
        println("xxx: $entry")

        // making it this far, markup is a key that came from this collection. The following line must
        // be true
        // ALWAYS FAILS
        assert(markupFeatureCollection.containsKey(markup))
    }
}

In the code above, feature is guaranteed to be non-null for incoming entry. For some reason if this fails I'm looking up the entry by its id. Even though I've retreived the key directly from the map, the runtime asserts pass, assert(markupFeatureCollection.containsKey(markup)) still always fails.

Why is this happening. Same behavior using mutableScatterMapOf()

Edit - I can't provide the entire source but this should help.

abstract class Markup(
    open val uuid: String,
    open val name: String,
    open val geoJson: JsonObject
)

data class Waypoint(
    override val uuid: String = UUID.randomUUID().toString(),
    override val name: String = createDefaultName(),
    override val geoJson: JsonObject
):Markup(
    uuid = uuid,
    name = name,
    geoJson = geoJson,
)

The hash key (the subject of this post) is a Waypoint. Neither Waypoint nor Markup provide overrides for hashCode() or equals().

All of the members of each are declared as val.

Update - I was naive in my assumption about how a hashmap captuers an object key hash. I don't want to override the equals and hashing functions, so I was able to compromise some perf and use the markup uuids instead. Here's the unit test for exploring the behavior.

    @Test
    fun `hashmap computed key test`(){
        data class Waypoint(
            val uuid: String = UUID.randomUUID().toString(),
            val geoJson: JsonObject = JsonObject()
        )

        val waypoint = Waypoint()

        val hashMap = HashMap<Waypoint, String>()
        hashMap[waypoint] = "waypointFeature"

        assertTrue(hashMap.containsKey(waypoint))
        assertTrue(hashMap[waypoint] == "waypointFeature")

        waypoint.geoJson.addProperty("locationName", "Home")

        // because geoJson's hash identity has changed, waypoint is no longer a valid key into the hash,
        // and the hash entry is now unreachable even when try to use the the actual hash entry as a key.
        assertFalse(hashMap.containsKey(waypoint))
        assertFalse(hashMap[waypoint] == "waypointFeature")

        val entry = hashMap.filter { it.key.uuid == waypoint.uuid }.keys.first()

        assertTrue(waypoint == entry)
        assertFalse(hashMap.containsKey(entry))
        assertTrue(hashMap[waypoint] == null)

        entry.geoJson.remove("locationName")

        // restored the ability to reach our entry by key
        assertTrue(hashMap.containsKey(entry))
        assertTrue(hashMap[waypoint] == "waypointFeature")
    }

Solution

  • As the Javadoc for Map says: "great care must be exercised if mutable objects are used as map keys." In your case, the Markup class you use as a key is mutable, even though all its properties are val. It's "mutable" in the deep sense, because one of its properties is a JsonObject (presumably from Gson) which is itself mutable.

    The equals and hashCode methods of data classes work recursively: Markup.equals calls geoJson.equals, and Markup.hashCode calls geoJson.hashCode. When you add a new property to geoJson, it now has a new hashcode, and therefore can't be found in the HashMap by direct lookup, although it can be found by filtering.

    You can fix the issue by making Markup.uuid the key to your map, instead of the Markup object itself, assuming that every instance has a unique uuid. If that is not the case, then you will need to remove and reinsert the map entry every time you change its key.