AssociateBy - List to Map in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
associateBy is a Kotlin collection function that converts a list into a map, where we decide which property of each item becomes the key.
In this blog, we will learn about the Kotlin Collection Functions - associateBy that converts a list into a map.
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
There are many useful collection functions in Kotlin. It is good to know about those and use those based on the requirement. One of those collection functions is associateBy.
associateBy let us convert a list into a map.
Let's learn by example.
Consider a data class Contact like below:
data class Contact(val name: String, val phoneNumber: String)
And, a list of Contact:
val contacts = listOf(
Contact("Amit", "+9199XXX11111"),
Contact("Messi", "+9199XXX22222"),
Contact("Ronaldo", "+9199XXX33333"))
Now, let's use the associateBy function on this list of Contact to get a Map with the
keyasnamevalueasphoneNumber
val nameToNumberMap = contacts.associateBy( {it.name}, {it.phoneNumber})
println(nameToNumberMap)
This will print the following:
{Amit=+9199XXX11111,
Messi=+9199XXX22222,
Ronaldo=+9199XXX33333}
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
If we go through the source code, we will find the following definition:
inline fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V>
Note:
- Returns a
Mapcontaining the values provided byvalueTransformand indexed bykeySelectorfunctions applied to elements of the given collection. - If any two elements is having the same key returned by
keySelectorthen, the last one will get added to the map. - Maintain the original order of items.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Frequently Asked Questions
What happens if two elements have the same key in associateBy?
The last one gets added to the map. If any two elements return the same key from the keySelector, the later element overwrites the earlier one. So, the map keeps only one value for each key.
Does associateBy keep the original order of the list?
Yes. The associateBy function maintains the original order of the items. The resulting map has its entries in the same order as the elements of the list.
What do the two lambdas in associateBy do?
The first lambda is the keySelector, which picks the key for each element. The second lambda is the valueTransform, which picks the value. For a list of contacts, we can use the name as the key and the phone number as the value.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
