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 }  // 바꿀 수 있는 'associate()'
      listOf(1).associate { it to getValue(it) } // 바꿀 수 있는 'associate()'
  }

빠른 수정을 적용한 후:

  fun getKey(i: Int) = 1L
  fun getValue(i: Int) = 1L

  fun test() {
      arrayOf(1).associateBy { getKey(it) }
      listOf(1).associateWith { getValue(it) }
  }