filterKeys

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

Returns a map containing all key-value pairs with keys 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, "something_else" to 3)

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

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