<p>In this tutorial, we will talk about Kotlin <span style="color: #0000ff;"><strong>Map</strong></span> with the help of simple examples.</p>
<h3 class="p1"><strong><span style="color: #000080;">Map</span></strong></h3>
<p class="p1">Maps are used to store <span style="color: #008000;"><b>key and value pairs</b></span>. In Map:</p>
<ul class="ul1">
<li>key and value are user defined.</li>
<li class="li1">The <span style="color: #008000;"><strong>key</strong></span> should be unique and only one value can be stored for each unique key.</li>
<li class="li1"><strong><span style="color: #008000;">Values</span></strong> can be duplicate.</li>
<li>A key value pair is also known as an <strong><span style="color: #0000ff;">entry</span></strong>.</li>
<li class="li1">The <span style="color: #000000;">key</span> and value can be of different <strong><span style="color: #008000;">data type</span></strong>.</li>
</ul>
<p>Map are of two types, <strong>immutable</strong> map (cannot be modified) and <strong>mutable</strong> map (can be modified).</p>
<h3><span style="color: #000080;"><strong>Immutable Map</strong></span></h3>
<p>The <span style="color: #0000ff;"><strong>immutable</strong></span> map also known as <span style="color: #0000ff;"><strong>read-only</strong></span> map support only read-only access to the map.</p>
<p>Immutable map are created using <strong><span class="s1" style="color: #000080;">mapOf()</span></strong> and <strong><span class="s1" style="color: #000080;">mapOf<;K, V>;()</span></strong> functions.</p>
<h4><span style="color: #000080;"><strong><span id="Create_List_MutableList_in_Kotlin">Create and initialize map </span></strong></span></h4>
<p>The below example show you how to:</p>
<ul class="ul1">
<li class="li1">initialize Map using <span style="color: #0000ff;"><strong><span class="s1">mapOf()</span></strong></span> (contains key and value of any data type), <span style="color: #0000ff;"><strong><span class="s1">mapOf<;K, V>;()</span></strong></span> (contains keys and values of K and V data types).</li>
<li class="li1">create new Map from another Map with the same entries using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-map.html"><span style="color: #0000ff;"><strong>toMap()</strong> </span></a>function.</li>
</ul>
<pre>fun main() {
 val map1 = <span style="color: #0000ff;"><strong>mapOf</strong></span>("Rank" to 1, 1 to "First", 'A' to 4)
 val map2 = <strong><span style="color: #0000ff;">mapOf</span><span style="color: #0000ff;"><;Int, String>;</span></strong>(1 to "One", 2 to "Two", 3 to "Three", 4 to "Four")
 val map3 = map2.<strong><span style="color: #0000ff;">toMap()</span></strong>;
 println("map1: $map1")
 <span style="color: #008000;"><strong> // map1: {Rank=1, 1=First, A=4}</strong></span>
 println("map2: $map2")
 <strong><span style="color: #008000;">// map2: {1=One, 2=Two, 3=Three, 4=Four}
 </span></strong><span style="color: #008000;"><span style="color: #000000;">println("map3: $map3")</span></span> <strong><span style="color: #008000;">
 // map3: {1=One, 2=Two, 3=Three, 4=Four}</span></strong>
}</pre>
<h4><span style="color: #000080;"><strong>Map size</strong></span></h4>
<p class="p2">The below example show you how to:</p>
<ul class="ul1">
<li class="li2">find the size of a Map using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/size.html"><span style="color: #0000ff;"><strong>size</strong></span></a> property or <span style="color: #0000ff;"><strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html"><span class="s2">count()</span></a></span> </strong></span>method.</li>
</ul>
<pre>val marks = mapOf<;String, Int>;("Physics" to 80, "Maths" to 97, "Biology" to 89, "Chemistry" to 95)

println("Size of map: ${<strong><span style="color: #0000ff;">marks.size</span></strong>}")
<strong><span style="color: #008000;">//Size of map: 4</span></strong>
println("Size of map using count(): ${<strong><span style="color: #0000ff;">marks.count()</span></strong>}")
<strong><span style="color: #008000;">//Size of map using count(): 4</span></strong></pre>
<h4 id="retrieve-keys-and-values" data-toc="map-operations#retrieve-keys-and-values"><strong><span class="article__header" style="color: #000080;"><span class="article__title">Retrieve Map keys and values </span></span></strong></h4>
<p class="p2">The below example show you how to:</p>
<ul class="ul1">
<li>get the set of keys, values and entries of a map using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/keys.html"><strong><span style="color: #0000ff;">keys</span></strong></a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/values.html"><strong><span style="color: #0000ff;">values</span></strong></a> and <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/entries.html"><strong><span style="color: #0000ff;">entries</span></strong></a> property.</li>
<li class="li2">access the value at specified key in a map using operator<span style="color: #0000ff;"><strong> []</strong>, <strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/get.html">get()</a>, <a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-value.html">getValue()</a></strong>, <a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-else.html"><strong><span class="s2">getOrElse()</span></strong></a>, <strong><span class="s2"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/get-or-default.html">getOrDefault()</a>.</span></strong></span></li>
</ul>
<pre>val marks = mapOf<;String, Int>;("Physics" to 80, "Maths" to 97, "Biology" to 89, "Chemistry" to 95)

println("Set of keys: ${marks.<strong><span style="color: #0000ff;">keys</span></strong>}")
<span style="color: #008000;"><strong>//Set of keys: [Physics, Maths, Biology, Chemistry]</strong></span>
println("Set of values: ${marks.<strong><span style="color: #0000ff;">values</span></strong>}")
<strong><span style="color: #008000;">//Set of values: [80, 97, 89, 95]</span></strong>
println("Set of entries: ${marks.<strong><span style="color: #0000ff;">entries</span></strong>}")
<strong><span style="color: #008000;">//Set of entries: [Physics=80, Maths=97, Biology=89, Chemistry=95]</span></strong>
println("Marks in Physics: ${marks.<strong><span style="color: #0000ff;">get</span></strong>("Physics")}")
<strong><span style="color: #008000;">//Marks in Physics: 80</span></strong>
println("Marks in English: ${<strong><span style="color: #0000ff;">marks["English"]</span></strong>}")
<strong><span style="color: #008000;">//Marks in English: null</span></strong>
println("Marks in Maths: ${marks.<strong><span style="color: #0000ff;">getValue</span></strong>("Maths")}")
<strong><span style="color: #008000;">//Marks in Maths: 97</span></strong>
println("Marks in English: ${marks.<strong><span style="color: #0000ff;">getOrDefault</span></strong>("English", -1)}")
<strong><span style="color: #008000;">//Marks in English: -1</span></strong>
println("Marks in English: ${marks.<strong><span style="color: #0000ff;">getOrElse</span></strong>("English", { null })}")
<strong><span style="color: #008000;">//Marks in English: null</span></strong></pre>
<h4 class="p2"><span style="color: #000080;"><strong>Map Contains Key or Values</strong></span></h4>
<p class="p2">The below example show you how to:</p>
<ul class="ul1">
<li>check if a map contains the specified <span style="color: #008000;"><strong>key </strong><span style="color: #000000;">using</span></span><strong><span style="color: #008000;"><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/contains-key.html"> <span style="color: #0000ff;">containsKey()</span></a>, <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains.html"><span style="color: #0000ff;">contains()</span></a>, <span style="color: #0000ff;">Key in Map.</span></span></strong></li>
<li>check if the map contains the specified <span style="color: #0000ff;"><strong>value </strong><span style="color: #000000;">using</span><strong> <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/contains-value.html">containsValue()</a></span></strong></span>.</li>
</ul>
<pre>val marks = mapOf<;String, Int>;("Physics" to 80, "Maths" to 97, "Biology" to 89, "Chemistry" to 95)

println("Does map contains Physics: ${marks.<strong><span style="color: #0000ff;">containsKey</span></strong>("Physics")}")
<span style="color: #008000;"><strong>//Does map contains Physics: true</strong></span>
println("Does map contains C++: ${marks.<span style="color: #0000ff;"><strong>contains</strong></span>("C++")}")
<strong><span style="color: #008000;">//Does map contains C++: false</span></strong>
println("Does map contains Chemistry: $<strong><span style="color: #0000ff;">{"Chemistry" in marks}</span></strong>")
<strong><span style="color: #008000;">//Does map contains Chemistry: true</span></strong>
println("Does any subject has 80 marks: ${marks.<strong><span style="color: #0000ff;">containsValue</span></strong>(80)}")
<span style="color: #008000;"><strong>//Does any subject has 80 marks: true</strong></span></pre>
<h4><strong><span style="color: #000080;">Map any function</span></strong></h4>
<p>The <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/any.html"><strong><span style="color: #0000ff;">any()</span></strong></a> method returns <span style="color: #008000;"><strong>true</strong></span> if at least one entry matches the given predicate.</p>
<pre>val marks = mapOf<;String, Int>;("Physics" to 80, "Maths" to 97, "Biology" to 89, "Chemistry" to 95)
val value = 80
val hasValue = marks.<span style="color: #0000ff;"><strong>any</strong></span> { it.value == value }

if (hasValue) {
 println("The map has value $value") <strong><span style="color: #008000;">//The map has value 80</span></strong>
} else {
 println("The map does not have value $value")
}</pre>
<h4 id="plus-and-minus-operators" data-toc="map-operations#plus-and-minus-operators"><span class="article__header"><span style="color: #000080;"><strong><span class="article__title">Map Plus and minus operators</span></strong></span></span></h4>
<p>The below example show you how to:</p>
<ul>
<li>replace or add an entry to a map from a given key-value pair or entries to a map from another map using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus.html"><span style="color: #0000ff;"><strong>plus() /operator(+)</strong></span></a>.</li>
<li>remove entry with the given key or a collection of keys: list, set, and so on using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus.html"><span style="color: #0000ff;"><strong>minus() /operator(-)</strong></span></a>.</li>
</ul>
<pre>val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)

println(numbersMap <span style="color: #0000ff;"><strong>+</strong></span> Pair("four", 4)) 
<span style="color: #0000ff;"><strong>//numbersMap.plus(Pair("four", 4))</strong></span>
<span style="color: #008000;"><strong>//{one=1, two=2, three=3, four=4}</strong></span>
println(numbersMap <strong><span style="color: #0000ff;">+</span> </strong>Pair("one", 10))
<strong><span style="color: #008000;">//{one=10, two=2, three=3}</span></strong>
println(numbersMap <strong><span style="color: #0000ff;">+</span></strong> mapOf("five" to 5, "one" to 11))
<span style="color: #0000ff;"><strong>//numbersMap.plus(mapOf("five" to 5, "one" to 11))</strong></span>
<span style="color: #008000;"><strong>//{one=11, two=2, three=3, five=5}</strong></span>
println(numbersMap <strong><span style="color: #0000ff;">-</span> </strong>"one") <strong><span style="color: #000080;"> 
<span style="color: #0000ff;">//numbersMap.minus("one")</span></span></strong>
<strong><span style="color: #008000;">//{two=2, three=3}</span></strong></pre>
<h4><span style="color: #000080;"><strong>Filter Map</strong></span></h4>
<p>The below example show you how to filter map:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li>using <span style="color: #0000ff;"><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html"><strong><span style="color: #0000ff;">filter()</span></strong></a><strong>: </strong><span style="color: #000000;">filter map entries using both keys and values.</span></span></li>
<li>using <span style="color: #0000ff;"><strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-keys.html">filterKeys()</a></span>: </strong><span style="color: #000000;">filter map entries using only keys.</span></span></li>
<li>using <span style="color: #0000ff;"><strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-values.html">filterValues()</a></span>: </strong><span style="color: #000000;">filter map entries using only values.</span></span></li>
</ul>
<pre>val items = mapOf("A" to 90, "B" to 80, "C" to 70, "D" to 60, "E" to 50)

val filtered = items.<span style="color: #0000ff;"><strong>filterKeys</strong></span> { it == "A" || it == "C" }
println(filtered) <span style="color: #008000;"><strong>//{A=90, C=70}</strong></span>
 
val filtered2 = items.<strong><span style="color: #0000ff;">filterValues</span></strong> { it >;= 70 }
println(filtered2) <strong><span style="color: #008000;">//{A=90, B=80, C=70}</span></strong>

val filtered3 = items.<strong><span style="color: #0000ff;">filter</span></strong> { it.key == "A" || it.value == 50 }
println(filtered3) <strong><span style="color: #008000;">//{A=90, E=50}</span></strong></pre>
<h3><span style="color: #000080;"><strong>Mutable Map</strong></span></h3>
<p><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/"><span style="color: #0000ff;"><strong>MutableMap</strong></span></a> inherites <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/"><span style="color: #0000ff;"><strong>Map</strong></span></a> and supports read/write access, you can add, update or remove entries.</p>
<p>Mutable map are created using <span style="color: #0000ff;"><strong>mutableMapOf()</strong></span> and <span style="color: #0000ff;"><strong>mutableMapOf()<;K,V>;()</strong> </span>functions.</p>
<h4><span style="color: #000080;"><strong><span id="Create_List_MutableList_in_Kotlin">Create and initialize MutableMap </span></strong></span></h4>
<p>The below example show you how to:</p>
<ul class="ul1">
<li class="li1">initialize MutableMap using <span style="color: #0000ff;"><strong>mutableMapOf() </strong></span>(contains key and value of any data type), <span style="color: #0000ff;"><strong>mutableMapOf()<;K,V>;()</strong></span> (contains keys and values of K and V data types).</li>
<li class="li1">create new MutableMap from another MutableMap with the same entries using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-mutable-map.html"><span style="color: #0000ff;"><strong>toMutableMap() </strong></span></a>function.</li>
</ul>
<pre> val map1 = <span style="color: #0000ff;"><strong>mutableMapOf</strong></span>("Rank" to 1, 1 to "First", 'A' to 4)
 val map2 = <strong><span style="color: #0000ff;">mutableMapOf</span></strong><;Int, String>;(1 to "One", 2 to "Two", 3 to "Three", 4 to "Four")
 val map3 = map2.<strong><span style="color: #0000ff;">toMutableMap()</span></strong>
 println(map1) <strong><span style="color: #008000;">// {Rank=1, 1=First, A=4}</span></strong>
 println(map2) <strong><span style="color: #008000;">// {1=One, 2=Two, 3=Three, 4=Four}</span></strong>
 println(map3) <strong><span style="color: #008000;">// {1=One, 2=Two, 3=Three, 4=Four}</span></strong>
}</pre>
<h4><span style="color: #000080;"><strong><span id="Add_items_to_List_in_Kotlin">Add items to MutableMap</span></strong></span></h4>
<p>The below example shows how to:</p>
<ul>
<li>add the specified key value pair to the map using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/put.html"><span style="color: #0000ff;"><strong>put(K,V)</strong></span></a> /<strong><span style="color: #0000ff;">operator</span> <span style="color: #0000ff;">[]</span></strong>. If key already exists it will replace the value.</li>
<li>add the specified key value pair to the map only if key does not exist earlier using <span style="color: #0000ff;"><strong>putIfAbsent()</strong></span>.</li>
<li>
<p class="p1">update a map with key/value pairs from the specified map using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/put-all.html"><span style="color: #0000ff;"><strong>putAll(Map<; K, V >;)</strong></span></a>.</p>
</li>
<li>append or replace all entries from the given map in a mutable map using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/plus-assign.html">plusAssign()</a>/operator(+=)</span></strong>.</li>
</ul>
<pre>val numMap = mutableMapOf<;Int, String>;(1 to "One", 2 to "Two")
numMap.<span style="color: #0000ff;"><strong>put</strong></span>(2, "two")
println(numMap) <span style="color: #008000;"><strong>//{1=One, 2=two}</strong></span>
<strong><span style="color: #0000ff;">numMap[3] = "three"</span></strong>
println(numMap) <strong><span style="color: #008000;">//{1=One, 2=two, 3=three}</span></strong>
numMap.putIfAbsent(4, "four")
println(numMap) <strong><span style="color: #008000;">//{1=One, 2=two, 3=three, 4=four}</span></strong>
numMap.<strong><span style="color: #0000ff;">putIfAbsent</span></strong>(1, "one")
println(numMap) <strong><span style="color: #008000;">//{1=One, 2=two, 3=three, 4=four}</span></strong>
numMap.<strong><span style="color: #0000ff;">putAll</span></strong>(mapOf(5 to "five"))
println(numMap) <strong><span style="color: #008000;">//{1=One, 2=two, 3=three, 4=four, 5=five}</span></strong>
numMap <strong><span style="color: #0000ff;">+=</span></strong> mapOf(6 to "six")
<span style="color: #0000ff;"><strong>//another way of doing numMap += mapOf(6 to "six")</strong>
<strong>//numMap.plusAssign(mapOf(6 to "six"))</strong></span>
println(numMap)
<strong><span style="color: #008000;">//{1=One, 2=two, 3=three, 4=four, 5=five, 6=six}</span></strong></pre>
<h4><span style="color: #000080;"><strong><span id="Kotlin_UpdateReplace_item_in_List">Update/Replace item in MutableList</span></strong></span></h4>
<p class="p1">The below example show you how to:</p>
<ul>
<li>replace the entry for the specified key only if it is currently mapped to some value <span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">using <span style="color: #0000ff;"><strong>replace( K , V ).</strong></span></span></li>
<li>replace the entry for the specified key only if currently mapped to the specified value using <span style="color: #0000ff;"><strong><span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">replace( K , oldV , newV)</span></strong></span>.</li>
</ul>
<pre>val numMap = mutableMapOf<;Int, String>;(1 to "One", 2 to "Two")
println(numMap) <span style="color: #008000;"><strong>//{1=One, 2=Two}</strong></span>
numMap.<span style="color: #0000ff;"><strong>replace</strong></span>(1, "one")
println(numMap) <strong><span style="color: #008000;">//{1=one, 2=Two}</span></strong>
numMap.<strong><span style="color: #0000ff;">replace</span></strong>(2, "Two", "two")
println(numMap) <strong><span style="color: #008000;">//{1=one, 2=two}</span></strong></pre>
<h4><span style="color: #000080;"><strong><span id="Kotlin_remove_items_from_List">Remove items from MutableList</span></strong></span></h4>
<p class="p1">The below example show you how to:</p>
<ul class="ul1">
<li class="li1">remove the specified key and its corresponding value from this map<br />
using<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html"> <span style="color: #0000ff;"><strong>remove( K )</strong></span></a>.</li>
<li class="li1">remove the entry with the given key from this mutable map<br />
using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/minus-assign.html"><strong><span style="color: #0000ff;">minusAssign() </span></strong></a><span style="color: #0000ff;"><span style="color: #000000;">or</span></span><strong><span style="color: #0000ff;"> operator(-=)</span></strong>.</li>
<li class="li1">remove the entry for the specified key only if it is mapped to the specified value<br />
using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/remove.html"><strong><span style="color: #0000ff;">remove( K, V ).</span></strong></a></li>
<li class="li1">remove all the items using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/clear.html"><span style="color: #0000ff;"><strong><span class="s2">clear()</span></strong></span></a></li>
</ul>
<pre>val numMap = mutableMapOf<;Int, String>;(1 to "One", 3 to "Three", 4 to "Four", 5 to "Five", 6 to "Six")
println(numMap) <strong><span style="color: #008000;">//{1=One, 3=Three, 4=Four, 5=Five, 6=Six}</span></strong>
numMap.<span style="color: #0000ff;"><strong>remove</strong></span>(6)
println(numMap) <span style="color: #008000;"><strong>//{1=One, 3=Three, 4=Four, 5=Five}</strong></span>
numMap <strong><span style="color: #0000ff;">-=</span></strong> 5
<span style="color: #0000ff;"><strong>//another way of doing numMap -= 5
//numMap.minusAssign(5)</strong></span>
println(numMap) <strong><span style="color: #008000;">//{1=One, 3=Three, 4=Four}</span></strong>
numMap.<strong><span style="color: #0000ff;">remove</span></strong>(4, "four")
println(numMap) <strong><span style="color: #008000;">//{1=One, 3=Three, 4=Four}</span></strong>
numMap.<strong><span style="color: #0000ff;">clear</span></strong>()
println(numMap) <span style="color: #008000;"><strong>//{}

</strong></span></pre>
<h4 class="p1"><span style="color: #000080;"><b>Iterate over Map in Kotlin</b></span></h4>
<p class="p2">The example show you how to iterate over a Map using:</p>
<ul class="ul1">
<li class="li2"><span style="color: #0000ff;"><strong>for</strong></span> loop</li>
<li><span class="s1" style="color: #0000ff;"><span class="s2"><strong>forEach()</strong></span></span><strong> </strong>function.</li>
<li><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map.html"><strong><span style="color: #0000ff;">map</span></strong></a> function.</li>
<li class="li2"><strong><span class="s1" style="color: #0000ff;"><span class="s2">Iterator</span></span> </strong>and a while loop.</li>
</ul>
<pre>val numMap = mapOf(1 to "One", 2 to "Two", 3 to "Three")

print("print map using for loop: ")
for ((key, value) in numMap) {
 print(" $key = $value ") 
}
println()

print("print map using forEach function: ")
numMap.forEach { (key, value) ->; print(" $key = $value ") }
println()

print("print map using map function: ")
numMap.map { (key, value) ->; print(" $key = $value ") }
println()

print("print map using Iterator: ")
val iterator = numMap.keys.iterator()

while (iterator.hasNext()) {
 val key = iterator.next()
 val value = numMap[key]
 print(" $key = $value ")
}
println()</pre>
<p><strong><span style="color: #0000ff;">Output:</span></strong></p>
<pre>print map using for loop: 1 = One 2 = Two 3 = Three
print map using forEach loop: 1 = One 2 = Two 3 = Three
print map using map function: 1 = One 2 = Two 3 = Three
print map using Iterator: 1 = One 2 = Two 3 = Three</pre>


