在给定条件下如何从MutableSet中删除项目? [英] How to remove an item from a MutableSet given a certain condition?
本文介绍了在给定条件下如何从MutableSet中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var chart_values: MutableSet<MutableMap.MutableEntry<String, Any>>? = mutableSetOf()
打印图表值:
[ground={},
ground_level={0=115, 1=4, 2=0, 3=37, 4=63, 5=44, 6=40, 7=9},
ground_over={0=3, 1=3, 2=3, 3=3, 4=3, 5=3, 6=3}
date_of_birth=1988-07-18T00:00Z]
我想从 chart_values
推荐答案
鉴于chartValues
类型为MutableSet<MutableMap.MutableEntry<String, Any>>?
,您可以执行以下操作以删除任何以空映射作为值的条目:
Given that chartValues
is of type MutableSet<MutableMap.MutableEntry<String, Any>>?
you can do the following to remove any entry with an empty map as a value:
chartValues?.removeAll { (_, value) ->
(value as? Map<*, *>)?.isEmpty() == true
}
as?
被称为安全强制转换运算符,它将返回强制转换的对象;如果强制转换失败,则返回null.
as?
is called safe cast operator and will return the casted object or null if the cast did not succeed.
注意:
- 使用
MutableMap<String, Any>
可能会更好
- 使用
val
而不是var
,因为您要更改集合而不是对其的引用
- You might be better off using a
MutableMap<String, Any>
- use
val
instead ofvar
, since you want to mutate the collection an not the reference to it
这篇关于在给定条件下如何从MutableSet中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文