Inspectopedia 2024.3 Help

'associate' can be replaced with 'associateBy' or 'associateWith'

Reports calls to associate() and associateTo() that can be replaced with associateBy() or associateWith().

Both functions accept a transformer function applied to elements of a given sequence or collection (as a receiver). The pairs are then used to build the resulting Map.

Given the transformer refers to it, the associate[To]() call can be replaced with more performant associateBy() or associateWith().

Examples:

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()' }

After the quick-fix is applied:

fun getKey(i: Int) = 1L fun getValue(i: Int) = 1L fun test() { arrayOf(1).associateBy { getKey(it) } listOf(1).associateWith { getValue(it) } }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

ReplaceAssociateFunction
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Kotlin | Style issues

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.3, Qodana for JVM 2024.3,

Can be installed with plugin:

Kotlin, 243.23126-IJ

Last modified: 03 December 2024