filter Values
inline fun <K, V> Map<out K, V>.filterValues(predicate: (V) -> Boolean): Map<K, V>
Content copied to clipboard
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.
Samples
import samples.*
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
val filteredMap = originalMap.filterValues { it >= 2 }
assertPrints(filteredMap, "{key2=2, key3=3}")
// original map has not changed
assertPrints(originalMap, "{key1=1, key2=2, key3=3}")
val nonMatchingPredicate: (Int) -> Boolean = { it == 0 }
val emptyMap = originalMap.filterValues(nonMatchingPredicate)
assertPrints(emptyMap, "{}")
//sampleEnd
}