java - How to use `filter` on Map instance in Kotlin? -
i see #filter
defined on map
, not able figure out how use it. care share example?
i have nested treemap instance (treemap<string, map<string, map<*, *>>>
) , want filter/find
first (which in domain) top-level key has characteristics associated deeper in value.
here's how data looks like:
{ "i1": { "aliases": {} }, "i2": { "aliases": {} }, "i3": { "aliases": {} }, "i4": { "aliases": { "alias-im-looking-for": {} } } }
i have following non-functional code solves right now:
val indextoaliasmappingtype = linkedtreemap<string, map<string, map<*, *>>>() val indextoaliasmappings = gson().fromjson(response.jsonstring, indextoaliasmappingtype.javaclass) var currentindexname = "" ((index, aliasmappings) in indextoaliasmappings) { val hascurrentalias = aliasmappings.get("aliases")?.containskey(alias) if (hascurrentalias != null && hascurrentalias) { currentindexname = index } } return currentindexname
looks need key part of first entry matches criteria. (single line intentional) should help:
val currentindexname = indextoaliasmappings.filtervalues { it["aliases"]?.containskey(alias) ?: false }.keys.firstornull() ?: ""
Comments
Post a Comment