How to Convert List to Map in Kotlin Using Kotlin Library

There are a few ways to convert a List to a Map in Kotlin by the Kotlin Library. In this post, we will be discussing, how we can convert List to Map using Kotlin Library.

Understanding the Problem

Before we dive into the Kotlin code, let’s first understand the problem we are trying to solve. Suppose you have a list of objects, and you want to convert this list into a map where each object is associated with a unique key. This is a common scenario in programming, especially when dealing with data that needs to be indexed or accessed quickly.

For instance, imagine you have a list of products, and each product has a unique ID. You might want to convert this list into a map where the product ID serves as the key, and the product object itself is the value. This transformation can simplify data retrieval and manipulation tasks.

Here’s a step-by-step guide on how to do it:

Using associate()

data class CourseList(val id :String, val courseName :String)


fun main(){

    val myCourseList = listOf(
        CourseList("1", "Android"),
        CourseList("2", "Kotlin"),
        CourseList("3","Java"),
        CourseList("4","C"),
        CourseList("5","C++")
    )

    println("list converted by using associate")
    val myCourseMap : Map<String, String> = myCourseList.associate {
        Pair(it.id, it.courseName)
    }

    println(myCourseMap)
}
list converted by using associate
{1=Android, 2=Kotlin, 3=Java, 4=C, 5=C++}

Using associateBy()

data class CourseList(val id :String, val courseName :String)

fun main(){

    val myCourseList = listOf(
        CourseList("1", "Android"),
        CourseList("2", "Kotlin"),
        CourseList("3","Java"),
        CourseList("4","C"),
        CourseList("5","C++")
    )

    println("list converted by using associateBy")
    val myCourseMap2 : Map<String, String> = myCourseList.associateBy(
        {it.id}, {it.courseName}
    )
    println(myCourseMap2)
}
list converted by using associateBy
{1=Android, 2=Kotlin, 3=Java, 4=C, 5=C++}

Using toMap()

data class CourseList(val id :String, val courseName :String)

fun main(){

    val myCourseList = listOf(
        CourseList("1", "Android"),
        CourseList("2", "Kotlin"),
        CourseList("3","Java"),
        CourseList("4","C"),
        CourseList("5","C++")
    )

    println("list converted by using toMap")
    val myCourseMap3 : Map<String, String> = myCourseList.map {
        it.id to it.courseName
    }.toMap()

    println(myCourseMap3)
}
list converted by using toMap
{1=Android, 2=Kotlin, 3=Java, 4=C, 5=C++}

Conclusion

In this guide, we’ve explored how to convert a list to a map in Kotlin using the Kotlin Library’s associateBy(), associate() and toMap() function. This powerful feature can streamline your code, making it more readable and efficient, especially when dealing with collections of data. By harnessing Kotlin’s capabilities, you can simplify complex data transformations and enhance your development workflow.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *