示例代码:
val map1: Map<String, DataClass> = ...
val map2: Map<String, DataClass> = ...
val result = map1 - map2在Android Studio中,result的类型被指示为Map<Any, DataClass>,这迫使我求助于:
val result = (map1 - map2).mapKeys { it.key.toString() }文档使我期望将类型K保留为String,或者我在这里遗漏了什么?
/**
* Returns a map containing all entries of the original map except those entries
* the keys of which are contained in the given [keys] collection.
*
* The returned map preserves the entry iteration order of the original map.
*/
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V> =
this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()发布于 2019-06-24 06:02:51
我的错;我显然没有仔细阅读文档。
修复:
val result = map1 - map2.keyshttps://stackoverflow.com/questions/56727936
复制相似问题