kotlin 两个map取交集 filterKeys

kotlin语言里面取两个map的交集,可以直接用filterKeys()方法,如下:

        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")

打印如下:

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}

你可能感兴趣的:(kotlin,kotlin,android,map,交集)