Avatar ·

How to convert a List to a Map in Kotlin: complete guide

Hello everyone! I just started learning Kotlin after Java, and I have a problem converting a list of objects to a Map. In Java, I used loops or Stream API with Collectors.toMap(), but in Kotlin I haven't figured out the correct syntax yet. I have a simple data class: ```kotlin data class User(val id: Int, val name: String) ``` And a list of users: ```kotlin val users = listOf( User(1, "Anna"), User(2, "Boris"), User(3, "Victor") ) ``` I need to convert this list to a Map, where the key will be the user's id and the value will be the User object itself. That is, I want to get: `{1=User(1, "Anna"), 2=User(2, "Boris"), 3=User(3, "Victor")}`. I tried writing something like `users.map { it.id to it }.toMap()`, but it looks weird and I'm not