Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: create a map from a list using index

Tags:

kotlin

I have a list as follows:

val numbers = listOf("one", "two", "three", "four")

I want to create a map such that each index goes to each value of the list.

1 to "one", 2 to "two", ..

I want the result to be a

Map<Int, String>
like image 709
Hossein Avatar asked Mar 02 '23 14:03

Hossein


1 Answers

One of the ways could be:

numbers.mapIndexed { index: Int, s: String -> index + 1 to s }.toMap()
like image 156
Sourav 'Abhi' Mitra Avatar answered Mar 05 '23 18:03

Sourav 'Abhi' Mitra