Kotlin

Kotlin List to Map

The Kotlin standard library includes a set of additional functions for collection transformations. Based on the transformation criteria specified, these functions introduce additional collections from existing ones. In the article, we will go through how to transform functions and are accessible. The map() method is the most basic mapping function; it applies a lambda type function to each consecutive element and returns a list of lambda type functions. Kotlin conveniently converts a list to the map form, which contains a list of complex forms and allows us to map components in the list to any value.

The syntax of the list, map in Kotlin?

To complete the kotlin operations in the program, we used various classes, functions, and variables in the kotlin language. The list to map conversion operation is one of the default ways to convert list data to map output.

variable1 = listOf()

variable2= variable1.method {code}. // use different methods

The basic syntax for performing and converting list data to map data is as follows. We may accomplish this by passing the values in a list format to lisOf ()and then utilizing different methods to transform the list values to map values.

How to transform a list into a map in Kotlin?

To understand the way of transforming the list to map in Kotlin language, we have some methods which we used in the following examples:

Example # 1: Using the associate function to transform list to map in Kotlin:

The associate () function is the most common approach to transforming a list into a map. It returns a map with key-value pairs generated by the transformation function on the specified list components. We will see how it works in the following example.


In the above code, we have created the data class name as “PersonList”. The data class “PersonList” has created a constructor and passed two parameters as variables. We have defined variables with the modifier “var”. The variables are named “Name” and “Age”. We have set the property type of these variables as String and kept them empty. Then, we have the main function declaration in which we have used variables as “val”. The “val” is now used for the “PersonList”.

We have initialized the variable with the list interface and assigned it with the elements for all the parameters of the “PersonList”. We have another variable as “myMap” with the modifier “val”. In the variable “myMap”, we created a map and added the list elements. We have invoked a function associated here, using another function, “pair”. The pair function will pair the two values of the same data type stored with the “it” keyword. The mapped List will be printed through the kotlin println function.

We get the following output of a map with a key-value structure when we run the code above.

Example # 2: Using the associateBy function to transform list to map in Kotlin:

Another function used to transform a list to Map is the AssociateBy function (). We will explore how to do it in the following example code.


The data class “student” is declared in the example code above. The primary constructor is called for this data class and passes two parameters. The parameters are the variables which are named “Name” and “Marks’ ‘and represented with the “var” keyword. These variables contain the String type property and are empty. After the data class declaration, we have a main function defined in which we have declared a variable “student” as “val” and set it with the list interface.

We have initialized the variable using the listOf function to create the list. In the listOf function, we have stored the different values of the parameters “Name” and “Marks”. Then, we defined a variable “myMap”, which creates a map containing the two String types in the angle brackets. We have used the associateBy function, which creates the maps out of the elements in a list referenced by a key. The keySelector argument specifies the key. Here the “it.Name” and “it.Marks” are the keySelector. Through the kotlin println function, we will print the map in which the initial list’s element iteration order is preserved.

The output displayed on the screen below is a map, and we have received it in key-value format.

Example # 3: Using the toMap function to transform list to map in Kotlin:

Another function in the Kotlin library converts a list of things into a Map. The toMap () function of the Kotlin Map class returns a new map having all the key-value pairs from a provided collection.


The code shown above has the data class called “Colors”. The data class “Colors” has a primary constructor, taking two variables as a parameter. The variables are represented with the keyword “var” and named “cName” and “serial”. These variables are assigned with the String property type, and they are initially empty.

Then, we have the main function where we have created a variable as “val ” and named it “Colors”. The variable “Colors” is initialized with the list of elements set for the parameter of the data class using the listOf function. After that we have created a variable “myMap” which will create a map. The keyselector “it.serial” is assigned to the “it.Name”. It means that each “it.serial” will have one “it.Name” value assigned. On the map function, we have invoked the “toMap” function, which will return the map containing all the values of the existing collection of lists. We have used the println function for printing the map format.

When we run the above code, it will produce the output, which is a map with a key-value structure.

Conclusion:

The article covers all the possible ways to transform the list into a map in the kotlin language. The basic introduction about Kotlin list to map, its syntax, how to transform a list to map works in Kotlin, and examples are covered. These methods are easy and understandable.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.