Updated April 6, 2023
Introduction to Kotlin Map
The kotlin map is one of the features, and it’s the generic collection interface. It is used for to store the elements in the key-value pairs, which is like java; the key-based access to the mapping entries are enabled with the various map-specific values, and the key must be the unique one it holds each key with the single value, and it also like the different pairs like either integer combine with string, integer and characters etc. the map interface is mutable so it can’t be changed, fixed size and the methods are only read access.
Syntax
The kotlin language used many features as similar to java like collection, util package and even other packages classes and its method behaviours are the same as java. A map is one of the interfaces that can hold the datas as key-value pairs like java.
fun main(args:Array<String>)
{
val vars:Map<Int,String> = mapOf<Int,String>(key to value)
-----some coding logics it depends upon the application requirement---
}
The above codes are the basic syntax for utilising the map interface on the kotlin. The mapping interface uses any type like Integer, String, Characters as the parameter type.
How does Map Function work in Kotlin?
A map is an interface that is used for to store the datas as the key-value pairs. Like java, it can be of the same type and feature. It’s worked in the kotlin. The key is the unique one, and it holds only a single value for each key; it also different pairs and combined with each other. It is the immutable one, and it is the fixed size, so after declaring the mapping size, it cannot be changed both static and dynamic. The members from the map interface are readable ones, so we cant edit and changeable for that. If we use the mutablemap interface, it supports both read and write access.
The key type is the invariant one, and it is accepted as the parameter; it uses a method like containsKey() and also it returns the keys set. Like that map value also covariant and its value type it also be changed if it’s required. The classes used on the map interface will be of any type; this is because of the classes used internally on the kotlin package. The map is of any type in the kotlin language; the key and value elements will be stored as a separate index; it takes the predicate arguments, the filter conditions will be of the negative type, and the key will be unique and it has many values.
Examples of Kotlin Map
Given below are the examples of Kotlin Map:
Example #1
Code:
package one;
fun main(args: Array<String>)
{
val map = mapOf(1 to "Siva", 2 to "raman" , 3 to "Welcome", 4 to "To", 5 to "My Domain", 12 to "Its the twelth index and key value", 13 to "Its the thirteen index and key value", 14 to "Its the fourteen index and key value", 15 to "Its the fifteen index and key value")
println( map)
println("Its the first example for reagrding the kotlin map interface we used different classes and methods that can be related to the interface")
val map1 = mapOf(6 to "Have a Nice Day users", 7 to "Its the Seventh index and key value" , 8 to "Its the eigth index and its key value", 9 to "Its the ninth index and its key value", 10 to "Its the tenth index and its key value", 11 to "Its the eleventh index and its key value")
println("Thank you users for to spenting the time with our application your entries are : "+map1)
println("Its the keys entries and it is used to store the variable : "+map1.keys )
println("Its the values the entries are used to store as the separate variable "+map1.values )
}
Output:
In the above example, we used a map interface, and it will store the user inputs as key-value pairs. We can use mapOf() method for to set the key values and print them accordingly.
Example #2
Code:
package one;
fun main(args: Array<String>)
{
val first = mapOf("siva" to 1, 1 to "raman", 'A' to "Welcome To My Domain", "Have a Nice Day Users" to 'A')
val second = mapOf<String, Int>("Employee ID" to 2001, "SNO" to 7536, "ProofID" to 12134)
for ((k,v) in first) {
println("Your input key is: $k and your input value is $v")
}
second.forEach { (k, v) ->
println("Your input key is: $k and your input value is $v")
}
val third = mapOf<String, Int>("Employee ID" to 2001, "SNO" to 7536, "ProofID" to 12134)
println("Your input map size is: ${third.size}")
println("Your input entries are: ${third.entries}")
println("Your input key is: ${third.keys}")
println("Your input values are: ${third.values}")
println("Your Employeed ID is: ${third.get("Employee ID")}")
println("Your Serial No is: ${third["SNO"]}")
println("Your proofs are: ${third.getValue("ProofID")}")
println("Your Employeed ID contains: ${third.containsKey("Employee ID")}")
println("Your Serial No contains: ${third.contains("SNO")}")
println("Your ProofID contains: ${"Chemistry" in third}")
println("If any of the columns or attributes missing please add it separately: ${third.containsValue(1234566)}")
println("The Additional or Missing attribute inputs are: ${third.getOrDefault("pincode",600063)}")
}
Output:
In the second example, we used key-value entries in the mapOf() method, and using the for loop, we can iterate the key-value entries and print them on the console.
Example #3
Code:
package one;
fun main(args: Array<String>)
{
val third = mutableMapOf("Employee ID" to 1, 2 to "Employee Name", 3 to "Address", 4 to "City", 5 to "pincode")
val four = mutableMapOf<String, Int>("MobileNumber" to 12435, "zipcode" to 27346, "SNo" to 1234)
println(third)
println(four)
val five = mutableMapOf<String, Int>("MobileNumber" to 2436, "zipcode" to 27346, "SNo" to 1234, "landline" to 91)
five.put("zipcode", 27346)
println(five)
five["SNo"] = 1234
println(five)
five.putIfAbsent("landline", 91)
five.putIfAbsent("MobileNumber", 2436)
println(five)
five.replace("Employee ID", 1)
println(five)
five.replace("Employee Name", 2, 3)
println(five)
five.remove("Address")
println(five)
five.remove("City", 4)
println(five)
five.clear()
println(five)
}
Output:
In the final example, we used a mutable map interface for to store the user inputs on key-value pairs. We used additional methods like remove(), replace(), clear() etc for to perform the operations.
Conclusion
In kotlin, we used a lot of collection util packages for to store, extract and retrieve the datas. Based on the requirements, we used specific util packages like map, list in a map we used a different set of interfaces and its methods for to implement the application which depends on the user requirements.
Recommended Articles
This is a guide to Kotlin Map. Here we discuss the introduction, syntax, and working of the map function in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –