Updated April 18, 2023
Introduction to Kotlin List to Map
Kotlin list to map is one of the features to convenient the list data to map using the built-in method like toMap() which given a list of complex objects will allow us to have the elements in our mapped list by using any type of values with the help of built-in method it requires us to transform the Array into the Pairs then later it will transfer the data to the map so this operation will be performed particular usages if we’re already operated on the collection of pairs value implemented using the associative API’s.
Syntax
In kotlin language, we used many default classes, methods, and variables to achieve the kotlin tasks in the application. The list to map is one of the conversion operations for performing the list data to the map data with the help of default methods.
fun methodName()
{
val x= listOf(values)
val y= x.map{ -- logic codes--}.toMap()
--some logic operations based on the needs—
}
fun main()
{
}
The above codes are the basic syntax for performing and convert the list data to the map data. We can achieve this operation using lisOf() to pass the values in the list format and then we use the .map() and toMap() method to convert the list values to the map values.
How does list to map work in Kotlin?
The list and map is the type of collection interface and it is used to store and retrieve the values from front end to back end and vice versa. Kotlin offers the convenience and performs the operations to convert the list data to the map type which has the list of complex objects to allow us to have elements in the list mapped by any type of value. The basic mapping function is the map() method and it applies the lambda type of function to each subsequent element and it returns the list of the lambda type of functions. Generally, the kotlin standard library provides the set of extension functions for data collection transformations and these functions build new collections from the existing thing and based on the data transformation rules provided from the library. The mapping transformation will create the collection from the list results of each type of the functions on the elements from one type of collection data to another type. So that it applies the lambda functions for each subsequent element and returns the list of the lambda results. The order of the result data is the same as the original data of the elements.
Example #1
Code:
data class firstlistExample(var strinps: String, var lnginps: Long);
fun main(args: Array<String>) {
println("Welcome To My Domain its the first example that related to the kotlin list to map")
var lst: List<firstlistExample> = listOf(
firstlistExample("This is the January Month", 1),
firstlistExample("This is the February Month", 2),
firstlistExample("This is the March Month", 3),
firstlistExample("This is the April Month", 4),
firstlistExample("This is the May Month", 5),
firstlistExample("This is the June Month", 6),
firstlistExample("This is the July Month", 7),
firstlistExample("This is the August Month", 8),
firstlistExample("This is the September Month", 9),
firstlistExample("This is the October Month", 10),
firstlistExample("This is the November Month", 11),
firstlistExample("This is the December Month", 12)
);
println("Your list collection values are listed: $lst")
val maps : Map<String, Long> = lst.associate { Pair(it.strinps, it.lnginps) };
println("Your list values are used in the map interface and it will paired to the method while passing the same parameter : $maps")
val maps2 : Map<String, firstlistExample> = lst.associate { Pair(it.strinps, it) };
println("The list values are iterated and converted to the map interface : $maps2")
}
Output:
In the above example, we used to list and map operations in the different scenarios. Like that we created a data class with the parameters like string and long type values. In the main method, we create the list instance with the class type, and using the listOf() method we can pass the class name with the parameter values. Here I used two types like string and long type first parameter is the string type values i.e) we can enter the month details as the first argument type and another parameter we used long to type it may be of numbers. So that I can start the values as 1 to 12 so that the twelve months are calculated and entered the input as the list collection. Then I created a map as another collection interface with the parameter as String and class name. The associate is another method of the list collection and passing the class parameter values and store them as a separate variable.
Example #2
Code:
fun main() {
println("Welcome To My Domain its the second example that related to the kotlin list to map collection")
val tv = listOf("Samsung TV", "Onida TV", "BPL TV", "Airtec", "Airtel","Sony","Vu","Thomson","Panasonic")
println(tv.associateBy { it.first().toUpperCase() })
println(tv.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))
val computer = listOf("Samsung PC", "Lenovo PC", "NEC", "Fujitsu", "Apple","Dell","HCL","HP","Acer")
println(computer.associateBy { it.first().toUpperCase() })
println(computer.associateBy(keySelector = { it.first().toUpperCase() }, valueTransform = { it.length }))
}
Output:
In the above example, we used the same previous example, here we declared and created the list in the main method. There is no object creation for the list collection, instead, we used the listOf() method for passing the user inputs. Now I can use the string inputs like a collection of tv manufacturers in the world. And after entering the inputs using the associateBy() method we can iterate the list values and convert it into map collection. Like that we can create and passing the list of computer manufacturers and using the same method we can iterate and pass the list values for mapping conversion.
Conclusion
In kotlin language, we have used many different concepts and features to implement the application with user requirements. Based on that the SDLC and here the android apps will be marketed among that list to map is the collection type for performing the user data operations from the application.
Recommended Articles
This is a guide to Kotlin List to Map. Here we discuss the Introduction, syntax, and working of the list to map in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –