Updated April 10, 2023
Introduction to Kotlin mapOf
The kotlin mapOf is defined as the function and it is one of the util collections and it is denoted under the interface it accepts keys and values for the user-defined types the key based mapped values are processing at the capabilities for to store and retrieving the values with the help of specific keys it also been separate and filter the keys and values mapping functions from the standard library which returns the keys and values from the map by using the filter() function we can filter the map keys and values if suppose the multiple pairs have the same key the values for the keys is from last of those pairs.
Syntax
In kotlin language, we have used many default keywords, functions, and other default classes based on the user requirement. The mapOf() method is one of the immutable maps that have to be created and it is a read-only format map that holds the data with the key-value pairs.
fun main(args:Array<String>)
{
val variablename=mapOf(key, values)
---some kotlin programming logic codes depends upon the requirement---
}
The above codes are the basic syntax for utilizing the mapOf() method in the kotlin application. We can also implement the application with the help of kotlin default packages.
How does mapOf() function work in Kotlin?
The mapOf() method helps to creates the read-only format and map value with the specified data contents on the list of pairs where the first index value is on the key format and the second index format is on the value. Generally, the map is one of the collection and that can behold and pairs of objects and it is based on immutable maps that can be read-only format it also looks for the map size it has a number of pairs and it can be determined with the property size and the default method like count() and size() method. These methods are used to count the characters and it has calculated the sizes these entries are keys and values also some other default methods like contains and get the values from the mapOf() method which helps to iterate the values by using forEach() loop. The map interface used to creates the new map that contains the new entry values it does not mutate the original map. If we want to add the map entries to the original map by using default methods like put() method for to insert the user input entries on the memory. With an array we can fetch the value by using the index which has to be an integer data type and all indexes have to be the sequential orders.
Examples of Kotlin mapOf
Examples of Kotlin mapOf are given below:
Example #1
Code:
package one;
enum class First(val exampl: Boolean = true){
Monday,
Tuesday,
Wednesday,
Thrusday,
Friday,
Saturday,
Sunday;
companion object{
fun demo(obj: First): Boolean {
return obj.name.compareTo("Tuesday") == 0 || obj.name.compareTo("Friday") == 0
}
}
}
fun demos(fr: First) {
when(fr) {
First.Monday -> println("Monday")
First.Tuesday->println("Tuesday")
First.Wednesday -> println("Wednesday")
First.Thrusday -> println("Thrusday")
First.Friday -> println("Friday")
First.Saturday -> println("Saturday")
First.Sunday ->println("Sunday")
}
}
fun main() {
println("Welcome to my domain its a first example regarding the kotlin mapOf() function")
val frs = mapOf(123 to "Monday", 345 to "Tuesday", 678 to "Wednesday",901 to "Thrusday",213 to "Friday",141 to "Saturday",516 to "Sunday")
println(frs)
val sec = mapOf("name" to "Sivaraman", "age" to "31", "address" to "Tiruppur", "City" to "tiruppur")
println(sec)
println("Thank you users have a nice day please keep and spent time with our application")
}
Output:
In the above example, we used enum concepts and the values are called through the main method after utilising the mapOf() method in there.
Example #2
Code:
package one;
fun main() {
println("Welcome to my domain its a first example regarding the kotlin mapOf() function")
val second = mapOf("january" to 1, "february" to 2, "march" to 3,
"april" to 4 , "may" to 5, "june" to 6, "july" to 7, "august" to 8, "september" to 9, "october" to 10, "november" to 11, "december" to 12)
var strs = "march"
if (second.containsKey(strs)) {
println("Thank you for your input strs $strs")
} else {
println("Please enter the input in string characters strs $strs")
}
val nums = 4
if (second.containsValue(nums)) {
println("Thank you for your input nums $nums")
} else {
println("Please enter the input in number format $nums")
}
val nums2 = mapOf<String, Int>("First String" to 1, "Second String" to 2, "Third String" to 3, "Fourth String" to 4,"Fifth String" to 5)
println("Your map size is: ${nums2.size}")
println("Your Entries list : ${nums2.entries}")
println("Your keys list: ${nums2.keys}")
println("Your values list: ${nums2.values}")
println("nums2 in First String: ${nums2.get("First String")}")
println("nums2 welcome: ${nums2["users"]}")
println("nums2 in Second String: ${nums2.getValue("Second String")}")
println("Is it map contains First String: ${nums2.containsKey("First String")}")
println("Is it map contains Third: ${nums2.contains("Third String")}")
println("Is it map contains Fourth: ${"Fourth String" in nums2}")
println("Is it map contains Fifth: ${"Fifth String" in nums2}")
println("Is it your collection total has integer nums2: ${nums2.containsValue(3)}")
println("nums2 in second string: ${nums2.getOrDefault("second input strings",0)}")
println("Thank you users have a nice day please keep and spent time with our application")
}
Output:
In the second example, we can validate the user inputs by using the conditional statements after registered on the mapOf() method.
Example #3
Code:
package one;
fun main() {
println("Welcome to my domain its a third example regarding the kotlin mapOf() function")
val third = HashMap<String, Int>()
third["Employee ID"] = 123
third["Employee PinCode"] = 600063
third["Employee Mobile"] = 822023
for ((k, v) in third) {
println("Thank you users your inputs are $k = $v")
}
val news = mapOf("EmployeeID" to 456, "Employee PinCode" to 654332, "Employee Mobile" to 98654)
println("Thank you users your Entries list are: " + news.entries)
println("Your input Keys are:" + news.keys)
println("Your input Values are:" + news.values)
println("Thank you users have a nice day please keep and spent time with our application")
}
Output:
In the final example, we used the hash map concept additionally and we stored the employee details with same as mapOf() method.
Conclusion
In the kotlin language, we used many different packages which contain the classes with default methods. Like that mapOf() is one of the default methods under the map interface with util collection package which is used to store the user inputs on the key-value pairs and it is an immutable one so we can’t change it.
Recommended Articles
This is a guide to Kotlin mapOf. Here we discuss the introduction, syntax, and working of mapOf() function in Kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –