inline fun <K, V> Map<out K, V>.filterValues( predicate: (V) -> Boolean ): Map<K, V>
Returns a map containing all key-value pairs with values matching the given predicate.
The returned map preserves the entry iteration order of the original map.
import kotlin.test.*
import java.util.*
fun main(args: Array<String>) {
//sampleStart
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
val filteredMap = originalMap.filterValues { it >= 2 }
println(filteredMap) // {key2=2, key3=3}
// original map has not changed
println(originalMap) // {key1=1, key2=2, key3=3}
val nonMatchingPredicate: (Int) -> Boolean = { it == 0 }
val emptyMap = originalMap.filterValues(nonMatchingPredicate)
println(emptyMap) // {}
//sampleEnd
}
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-values.html