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")
    }

20

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.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật