Kotlin two maps take the intersection filterKeys

In the kotlin language, to take the intersection of two maps, you can directly use the filterKeys() method, as follows:

        val map1 = mapOf("1" to "1", "2" to "2")
        val map2 = mapOf("2" to "2", "3" to "3")
        val map3 = map1.filterKeys {
            map2.keys.contains(it)
        }
        Log.d("Alex", "map1:  $map1")
        Log.d("Alex", "map2:  $map2")
        Log.d("Alex", "map3:  $map3")

It prints as follows:

2023-04-03 08:48:43.870 1934-1934/com.example.myapplication D/Alex: map1:  {1=1, 2=2}
2023-04-03 08:48:43.870 1934-1934/com.example.myapplication D/Alex: map2:  {2=2, 3=3}
2023-04-03 08:48:43.870 1934-1934/com.example.myapplication D/Alex: map3:  {2=2}

Guess you like

Origin blog.csdn.net/msn465780/article/details/129921326