且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在给定的Kotlin键列表中对地图进行切片

更新时间:2023-11-27 09:37:40

您可以使用

You can use filterKeys:

val ab = abc.filterKeys { it in keys }

由于它是Kotlin,您甚至可以定义自己的扩展功能完全达到您的想象:

And since it is Kotlin, you could even define your own extension function to achieve exactly what you imagined:

fun <T> Map<T, *>.slice(keys: Iterable<T>) = filterKeys { it in keys }

val ab = abc.slice(keys)