Create an immutable map using the mapOf function and insert different pairs that contain init as a key and a string as a value:
val map: Map<Int,String> = mapOf( Pair(1,"One"), Pair(2,"Two"), ,Pair(3,"Three"), 4 to "Four", 5 to "Five")
Take a look at the following diagram of the Map interface:
We can access each pair using a for loop. Pairs provide two properties: key and value. Take a look at the following example, which uses a for loop to print each pair:
for(pair in map) {
println("${pair.key} ${pair.value}")
}
Like sets, maps do not support duplicate values. We can check this by adding more than one pair of the same type and printing the map on the screen:
val map: Map<Int,String> = mapOf( Pair(1,"One"), Pair(1,"One"), Pair(2,"Two"), Pair(3,"Three"), 4 to "Four", 5 to "Five")
Here, the map will only contain one Pair of the (1, "One") value.
Maps provide a number of useful functions and properties:
- Size: This property returns the size of a map.
- isNotEmpty(): This returns true if the map is empty. Otherwise, it returns false:
if( map.isNotEmpty()) {
println("Map size is ${map.size}" )
}
- Keys and values: These properties return the set of keys and the set of values:
val setofkeys = map.keys
println("Keys $setofkeys")
val setofvalues = map.values
println("Values $setofvalues")
- Entries: This property returns a set of pairs:
val setOfPairs = map.entries
for ((key, value) in setOfPairs) {
println("$key $value")
}
- get(key): The get function takes a key as a parameter and fetches the value from the list. It returns null if the key does not exist.
- containsKey(key): The containsKey function takes key as a parameter. It returns true if the key exists, otherwise it returns false:
var key = 1
if(map.containsKey(key)) {
val value = map.get(key)
println("key: $key value: $value")
}
As we know, the get function will return null if map does not contain the key, so it is necessary to provide a nullable data type. Create a function that takes an integer as a parameter and returns a string. The daysOfWeek map contains an init and string pair, and the get function returns a value against key, which is the number of the day. If the get function cannot find a key, it will return null. To make this function compilable, we must declare the result and the function return type to be nullable (?):
fun mapDaysOfWeek(day: Int): String? {
var result : String?
val daysOfWeek: Map<Int, String> = mapOf(1 to "Monday", 2 to "Tuesday", 3 to "Wednesday", 4 to "Thrusday", 5 to "Firday", 6 to "Saturday", 7 to "Sunday")
result = daysOfWeek.get(day)
return result
}
- getOrDefault(): To avoid the confusion of the nullable variable, we can use getOrDefault. While the get function returns null if the key does not exist, this function returns a default value instead:
fun mapDaysOfWeek(day: Int): String {
var result : String
val daysOfWeek: Map<Int, String> = mapOf(1 to "Monday", 2 to "Tuesday", 3 to "Wednesday", 4 to "Thrusday", 5 to "Firday", 6 to "Saturday", 7 to "Sunday")
result = daysOfWeek.getOrDefault(day, "Invalid input")
return result
}
fun main(args: Array<String>) {
var result = mapDaysOfWeek(1)
println(result)
result = mapDaysOfWeek(9)
println(result)
}
Here, the first function call will return Monday and the second function call will return Invalid input.