filterKeys所在位置是kotlin.collections.filterKeys,其相关用法介绍如下。

用法:

inline fun <K, V> Map<out K, V>.filterKeys(
    predicate: (K) -> Boolean
): Map<K, V>

返回包含与给定 predicate 匹配的键的所有键值对的映射。

返回的映射保留原始映射的条目迭代顺序。

例子:

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val originalMap = mapOf("key1" to 1, "key2" to 2, "something_else" to 3)

val filteredMap = originalMap.filterKeys { it.contains("key") }
println(filteredMap) // {key1=1, key2=2}
// original map has not changed
println(originalMap) // {key1=1, key2=2, something_else=3}

val nonMatchingPredicate: (String) -> Boolean = { it == "key3" }
val emptyMap = originalMap.filterKeys(nonMatchingPredicate)
println(emptyMap) // {}
//sampleEnd
}

输出:

{key1=1, key2=2}
{key1=1, key2=2, something_else=3}
{}