Industrial Training




Kotlin Map Interface


Kotlin Map is an interface and generic collection of elements. Map interface holds data in the form of key and value pair. Map key are unique and holds only one value for each key. The key and value may be of different pairs such as < Int, Int>,< Int, String>, < Char, String>etc. This interface is immutable, fixed size and its methods support read only access.
To use the Map interface we need to use its function called mapOf() or mapOf< k,v>().


Map Interface Declaration


interface Map< K, out V> (source)  
interface Map< K, out V> (source)

Properties of Map Interface


Properties Description
abstract val entries: Set< Entry< K, V>> It returns only read all key and value pair of Set Interface in current map.
abstract val keys: Set< K> It returns only read all key of Set Interface in current map.
abstract val keys: Set< K> It returns the number of key and value pair in current map.
abstract val values: Collection< V> It returns only read Collection of all valued in current map. This collection may contain duplicate values.

Functions of Map Interface


There are several functions are available in Map interface. Some functions of Map interface are mention below.


Functions Description
fun < K, V> Map< key, value>.getValue(key: K): V It returns a value of given key or throws an exception if no such key is available in the map.
operator fun < V, V1 : V> Map< in String, V>.getValue( thisRef: Any?, property: KProperty< *> ): V1 It returns the value of the property for the given object from current read- only map.
operator fun < K, V> Map< out K, V>.contains(key: K): Boolean It checks is the given key contains in map.
operator fun < K, V> Map< out K, V>.contains(key: K): Boolean It checks is the given key contains in map.
fun < K> Map< out K, *>.containsKey(key: K): Boolean If map contains the specified key it returns true.
fun < K, V> Map< K, V>.containsValue(value: V): Boolean If map maps one or more keys to specified value it returns true.
fun < K, V> Map< out K, V>.getOrDefault( key: K, defaultValue: V ): V It returns the value which is given by key in mapped, or returns default value if map dose not contains mapping for the given key.
fun < K, V> Map< out K, V>.asIterable(): Iterable< Entry< K, V>> It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated.
fun < K, V> Map< out K, V>.asIterable(): Iterable< Entry< K, V>> It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated.
fun < K, V> Map< out K, V>.asSequence(): Sequence< Entry< K, V>> It creates a Sequence interface instance which wraps the current map and returning its entries when it has iterated.
operator fun < K, V> Map< out K, V>.iterator(): Iterator< Entry< K, V>> It returns an Iterator over the entries in the Map.
operator fun Map.minus(key: K): Map It returns a map which contains all the entries of original map except the entry of mention key.
operator fun < K, V> Map< out K, V>.minus( keys: Iterable< K> ): Map< K, V> It returns a map which contains all the entries of original map except those entries key which are contained in the mention key collection.
operator fun < K, V> Map< out K, V>.minus( keys: Sequence< K> ): Map< K, V> It returns a map which contains all the entries of original map except those entries key which are contained in the given key sequence.
operator fun < K, V> Map< out K, V>.plus( pair: Pair< K, V> ): Map< K, V> It creates a new read only map by adding or replacing an entry to current map from a given key-value pair.
operator fun < K, V> Map< out K, V>.plus( pairs: Iterable< Pair< K, V>> ): Map< K, V> It creates a new read only map by adding or replacing entries to current map from a given collection of key-value pairs.
operator fun < K, V> Map< out K, V>.plus( pairs: Sequence< Pair< K, V>> ): Map< K, V> It creates a new read only map by adding or replacing entries to current map from a given sequence of key-value pairs.

Kotlin Map Interface Example 1


Let's create an example of declaring and traversing the value of map using mapOf< k,v>() function. In this example, we create key of Int and value of String types.


fun main(args: Array){  
  
    val myMap = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
    for(key in myMap.keys){  
        println(myMap[key])  
    }  
}  

Output:
Ajay
Vijay
Prakash

Kotlin Map Interface Example 2 - generic


For more specific we can provide generic type Map such as myMap: Map = mapOf< k,v>().


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
}  


Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash


Kotlin Map Interface Example 3 - non generic


If we cannot specify any types of key and value of Map Interface then it can take different types of key and value. This is because all class internally uses < Any, Any> types. For example:

fun main(args: Array){  
  
    val myMap = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash","ram" to "Ram", "two" to 2)  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
Element at key ram = Ram
Element at key two = 2


Kotlin Map Interface Example 4 - mapOf().getValue()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
    println(".....myMap.getValue(4).......")  
    println(myMap.getValue(4))  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.....myMap.getValue(4).......
Vijay


Kotlin Map Interface Example 5 - mapOf().contains()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
  
      println(".....myMap.contains(3).......")  
      println( myMap.contains(3))  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.....myMap.contains(3).......
true


Kotlin Map Interface Example 6 - mapOf().containsKey()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
  
      println("......myMap.containsKey(2)......")  
      println(myMap.containsKey(2))  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.containsKey(2)......
false

Kotlin Map Interface Example 7 - mapOf().containsValue ()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
         println("......myMap.containsValue(\"Ajay\")......")  
         println(myMap.containsValue("Ajay"))  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.containsValue("Ajay")......
true

Kotlin Map Interface Example 8 - mapOf().get()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
        println(".....myMap.get(1).......")  
        println(myMap.get(1))  
}  


Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.....myMap.get(1).......
Ajay

Kotlin Map Interface Example 9 - mapOf().getOrDefault ()


fun main(args: Array< String>){  
  
    val myMap: Map< Int,String> = mapOf< Int, String>(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
    
        println("......myMap.getOrDefault(3, \"Vijay\")......")  
        println(myMap.getOrDefault(3, "Vijay"))  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.getOrDefault(3, "Vijay")......
Prakash


Kotlin Map Interface Example 10 - mapOf().asIterable ()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
      println(".......myMap.asIterable().....")  
      for(itr in myMap.asIterable()){  
          println("key = ${itr.key} value = ${itr.value}")  
      }  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.......myMap.asIterable().....
key = 1 value = Ajay
key = 4 value = Vijay
key = 3 value = Prakash


Kotlin Map Interface Example 11 - mapOf().iterator()


fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
      println("......myMap.iterator()......")  
      for(itr1 in myMap.iterator()){  
          println("key = ${itr1.key} value = ${itr1.value}")  
      }  
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.iterator()......
key = 1 value = Ajay
key = 4 value = Vijay
key = 3 value = Prakash

Kotlin Map Interface Example 12 - mapOf().minus()


fun main(args: Array< String>){  
  
    val myMap: Map< Int,String> = mapOf< Int, String>(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
      println("......myMap.minus(4)......")  
      for(m in myMap.minus(4)){  
          println(myMap[m.key])  
      }  
}  


Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.minus(4)......
Ajay
Prakash

Kotlin Map Interface Example 13 - mapOf().plus()
fun main(args: Array){  
  
    val myMap: Map = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")  
  
    for(key in myMap.keys){  
        println("Element at key $key = ${myMap.get(key)}")  
    }  
      println("......myMap.plus(Pair(5, \"Rohan\"))......")  
      for(p in myMap.plus(Pair(5, "Rohan"))){  
          println("Element at key ${p.key} = ${p.value}")  
      }  
    
}  

Output:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.plus(Pair(5, "Rohan"))......
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
Element at key 5 = Rohan




Hi I am Pluto.