filterValues

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

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.

Since Kotlin

1.0

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 }
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
}