johnrao07
johnrao07

Reputation: 6908

Remove item from MutableMap inside a MutableSet

var chart_values: MutableSet<MutableMap.MutableEntry<String, Any>>? = mutableSetOf()

Printing chart_values:

[ground={}, 
ground_level={}, 
date_of_birth=1988-07-18T00:00Z]

I try to remove it with the below code

Activity.player.chart_values.remove('date_of_birth')

the above line shows up error without even running

Type inference failed. The value of the type parameter 
T should be mentioned in input types (argument types, 
receiver type or expected type). Try to specify it explicitly.

Upvotes: 1

Views: 728

Answers (2)

B&#246; macht Blau
B&#246; macht Blau

Reputation: 13009

chart_values is a MutableSet with elements of type MutableMap.MutableEntry<String, Any> So you can only remove a MutableMap.MapEntry from it, not a key to an entry of a MutableMap

You can iterate over the MutableMap.MapEntry elements of the MutableSet using an iterator and then try to remove the map entry with the given key.

var chart_values: MutableSet<MutableMap.MutableEntry<String, Any>>? = mutableSetOf()

fun removeMapEntry(mapKey: String): Boolean {
    val iterator: MutableIterator<MutableMap.MutableEntry<String, Any>> = chart_values?.iterator() ?: return false
    iterator.forEach { 
        // it: MutableMap.MutableEntry<String, Any>
        if (mapKey == it.key){
            iterator.remove()
            return true
        }
    }
    return false
}

Upvotes: 1

Ninja420
Ninja420

Reputation: 3872

Not sure how MutableMap.MutableEntry<String, Any> works but I would have done it using Pair

var chart_values: MutableSet<Pair<String, Any>> = mutableSetOf()
chart_values.removeIf { it.first == "date_of_birth" }

Upvotes: 0

Related Questions