associateBy()
或 associateWith()
的 associate()
和 associateTo()
的调用。
两个函数都接受应用于给定序列或集合 (作为接收器) 的元素的转换器函数。
然后该对函数被用于构建结果 Map
。
鉴于转换器引用 it
,associate[To]()
调用可以替换为性能更高的 associateBy()
或 associateWith()
。
示例:
fun getKey(i: Int) = 1L
fun getValue(i: Int) = 1L
fun test() {
arrayOf(1).associate { getKey(it) to it } // replaceable 'associate()'
listOf(1).associate { it to getValue(it) } // replaceable 'associate()'
}
在应用快速修复后:
fun getKey(i: Int) = 1L
fun getValue(i: Int) = 1L
fun test() {
arrayOf(1).associateBy { getKey(it) }
listOf(1).associateWith { getValue(it) }
}