Kotlin Map InterfaceKotlin 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 DeclarationProperties of Map Interface
Functions of Map InterfaceThere are several functions are available in Map interface. Some functions of Map interface are mention below.
Kotlin Map Interface Example 1Let'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. Output: Ajay Vijay Prakash Kotlin Map Interface Example 2 - genericFor more specific we can provide generic type Map such as myMap: Map<k, v> = mapOf<k,v>(). Output: Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash Kotlin Map Interface Example 3 - non genericIf 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: 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()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()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()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 ()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()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 ()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 ()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()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()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()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 Next TopicHashMap: hashMapOf() |