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

Properties of Map Interface

PropertiesDescription
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.

FunctionsDescription
fun <K, V> Map<key, value>.getValue(key: K): VIt 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): BooleanIt checks is the given key contains in map.
fun <K> Map<out K, *>.containsKey(key: K): BooleanIf map contains the specified key it returns true.
fun <K, V> Map<K, V>.containsValue(value: V): BooleanIf 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 funMap.minus(key: K): MapIt 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.

Output:

Ajay
Vijay
Prakash

Kotlin Map Interface Example 2 - generic

For 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 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:

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





Latest Courses