KT.EXPLICIT_COLLECTION_ELEMENT_ACCESS_METHOD

Usage of 'get' or 'set' can be replaced with indexed access operator []

In Kotlin functions get or set can be replaced with the shorter operator [], see https://kotlinlang.org/docs/operator-overloading.html#indexed-access-operator. Prefer the usage of the indexed access operator [] for map or list element access or insert methods.

Noncompliant Code

Copy
val map = Map<String, String>()
map.put("key", "value")
val value = map.get("key")

Compliant Code

Copy
val map = Map<String, String>()
map["key"] = "value"
val value = map["key"]

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html