Daniele B
06/02/2021, 2:05 PMmyList.removeAll { it.myField != myFieldValue }
But for a map, there doesn’t seem to be removeAll
:
myMap.removeAll { it.key != myKeyValue } <--- removeAll doesn't exist
what should I use instead?Luke
06/02/2021, 2:16 PMmyMap.filter { it.key == myKeyValue }
?Luke
06/02/2021, 2:17 PMDaniele B
06/02/2021, 2:19 PMval myMap : MutableMap<Int, MyObject>
so, being a val, a new map cannot be setilya.gorbunov
06/02/2021, 2:20 PMmyMap.keys.removeAll { it != myKeyValue }
Daniele B
06/02/2021, 2:23 PMephemient
06/03/2021, 7:22 AMmyMap.keys.retainAll { it == myKeyValue }
ephemient
06/03/2021, 7:25 AMval value = myMap[myKeyValue]
myMap.clear()
if (value != null) myMap[myKeyValue] = value
which doesn't require an external iteration