0
0
KotlinHow-ToBeginner · 2 min read

Kotlin How to Convert List to Map with Examples

In Kotlin, convert a list to a map using associateBy { keySelector } to create a map with keys from list elements or associate { key to value } to define both keys and values explicitly.
📋

Examples

Input["apple", "banana", "cherry"]
Output{"apple": "apple", "banana": "banana", "cherry": "cherry"}
Input["apple", "banana", "cherry"] with key = first letter
Output{"a": "apple", "b": "banana", "c": "cherry"}
Inputempty list []
Output{}
🧠

How to Think About It

To convert a list to a map, decide what you want as keys and values. Use associateBy if keys come from each element and values are the elements themselves. Use associate if you want to create both keys and values from each element.
📐

Algorithm

1
Get the input list
2
Choose how to create keys from list elements
3
Choose how to create values from list elements
4
Use <code>associateBy</code> if values are elements and keys are derived
5
Use <code>associate</code> if both keys and values are custom
6
Return the resulting map
💻

Code

kotlin
fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val mapByFirstLetter = fruits.associateBy { it.first() }
    println(mapByFirstLetter)

    val mapCustom = fruits.associate { it to it.length }
    println(mapCustom)
}
Output
{a=apple, b=banana, c=cherry} {apple=5, banana=6, cherry=6}
🔍

Dry Run

Let's trace converting list ["apple", "banana", "cherry"] to map by first letter

1

Start with list

["apple", "banana", "cherry"]

2

Extract key for each element

Keys: 'a' from "apple", 'b' from "banana", 'c' from "cherry"

3

Create map entries

{'a' to "apple", 'b' to "banana", 'c' to "cherry"}

ElementKeyValue
appleaapple
bananabbanana
cherryccherry
💡

Why This Works

Step 1: Using associateBy

associateBy creates a map where keys come from a function applied to each element, and values are the elements themselves.

Step 2: Using associate

associate lets you define both keys and values by returning pairs from each element.

Step 3: Resulting map

The output is a map where each key points to its corresponding value as defined by your functions.

🔄

Alternative Approaches

Using for loop and mutable map
kotlin
fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val map = mutableMapOf<Char, String>()
    for (fruit in fruits) {
        map[fruit.first()] = fruit
    }
    println(map)
}
More manual and verbose, but useful if you want to add complex logic during conversion.
Using map and toMap
kotlin
fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val pairs = fruits.map { it.first() to it }
    val map = pairs.toMap()
    println(map)
}
Creates pairs first then converts to map; good for clarity but less concise.

Complexity: O(n) time, O(n) space

Time Complexity

The conversion loops through the list once, so time grows linearly with list size.

Space Complexity

A new map is created with one entry per list element, so space grows linearly.

Which Approach is Fastest?

associateBy and associate are optimized and concise; manual loops may be slower and more verbose.

ApproachTimeSpaceBest For
associateByO(n)O(n)Simple key extraction
associateO(n)O(n)Custom keys and values
for loop with mutableMapO(n)O(n)Complex logic during conversion
map + toMapO(n)O(n)Clear pair creation before map
💡
Use associateBy for simple key extraction and associate when you need custom keys and values.
⚠️
Trying to convert a list with duplicate keys without handling duplicates causes unexpected overwrites in the map.