<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Map</id>
	<title>KOTLIN: Map - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=KOTLIN%3A_Map"/>
	<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;action=history"/>
	<updated>2026-04-23T09:02:26Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;diff=65905&amp;oldid=prev</id>
		<title>Unknown user at 04:07, 23 July 2022</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;diff=65905&amp;oldid=prev"/>
		<updated>2022-07-23T04:07:25Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;amp;diff=65905&amp;amp;oldid=65804&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Unknown user</name></author>
	</entry>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;diff=65804&amp;oldid=prev</id>
		<title>Unknown user: Created page with &quot;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_maps.htm    Kotlin map is a collection of key/value pairs, where each key is unique, and it can only be associated with on...&quot;</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=KOTLIN:_Map&amp;diff=65804&amp;oldid=prev"/>
		<updated>2022-07-18T02:39:51Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_maps.htm    Kotlin map is a collection of key/value pairs, where each key is unique, and it can only be associated with on...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sumber: https://www.tutorialspoint.com/kotlin/kotlin_maps.htm&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Kotlin map is a collection of key/value pairs, where each key is unique, and it can only be associated with one value. The same value can be associated with multiple keys though. We can declare the keys and values to be any type; there are no restrictions.&lt;br /&gt;
&lt;br /&gt;
A Kotlin map can be either mutable (mutableMapOf) or read-only (mapOf).&lt;br /&gt;
&lt;br /&gt;
Maps are also known as dictionaries or associative arrays in other programming languages.&lt;br /&gt;
&lt;br /&gt;
Creating Kotlin Maps&lt;br /&gt;
For map creation, use the standard library functions mapOf() for read-only maps and mutableMapOf() for mutable maps.&lt;br /&gt;
&lt;br /&gt;
A read-only view of a mutable map can be obtained by casting it to Map.&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    println(theMap)&lt;br /&gt;
    &lt;br /&gt;
    val theMutableMap = mutableSetOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    println(theMutableMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=1, two=2, three=3, four=4}&lt;br /&gt;
[(one, 1), (two, 2), (three, 3), (four, 4)]&lt;br /&gt;
Creating Map using HashMap&lt;br /&gt;
A Kotlin map can be created from Java&amp;#039;s HashMap.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = HashMap&amp;lt;String, Int&amp;gt;()&lt;br /&gt;
    &lt;br /&gt;
    theMap[&amp;quot;one&amp;quot;] = 1&lt;br /&gt;
    theMap[&amp;quot;two&amp;quot;] = 2&lt;br /&gt;
    theMap[&amp;quot;three&amp;quot;] = 3&lt;br /&gt;
    theMap[&amp;quot;four&amp;quot;] = 4&lt;br /&gt;
    &lt;br /&gt;
    println(theMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{four=4, one=1, two=2, three=3}&lt;br /&gt;
Using Pair while Creating Map&lt;br /&gt;
We can use Pair() method to create key/value pairs:&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(Pair(&amp;quot;one&amp;quot;, 1), Pair(&amp;quot;two&amp;quot;, 2), Pair(&amp;quot;three&amp;quot;, 3))&lt;br /&gt;
    println(theMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=1, two=2, three=3}&lt;br /&gt;
Kotlin Properties&lt;br /&gt;
Kotlin map has properties to get all entries, keys, and values of the map.&lt;br /&gt;
&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
    println(&amp;quot;Entries: &amp;quot; + theMap.entries)&lt;br /&gt;
    println(&amp;quot;Keys:&amp;quot; + theMap.keys)&lt;br /&gt;
    println(&amp;quot;Values:&amp;quot; + theMap.values)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Entries: [one=1, two=2, three=3, four=4]&lt;br /&gt;
Keys:[one, two, three, four]&lt;br /&gt;
Values:[1, 2, 3, 4]&lt;br /&gt;
Loop through Kotlin Maps&lt;br /&gt;
There are various ways to loop through a Kotlin Maps. Lets study them one by one:&lt;br /&gt;
&lt;br /&gt;
Using toString() function&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    println(theMap.toString())&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=1, two=2, three=3, four=4}&lt;br /&gt;
Using Iterator&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
    val itr = theMap.keys.iterator()&lt;br /&gt;
    while (itr.hasNext()) {&lt;br /&gt;
        val key = itr.next()&lt;br /&gt;
        val value = theMap[key]&lt;br /&gt;
        println(&amp;quot;${key}=$value&amp;quot;)&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
one=1&lt;br /&gt;
two=2&lt;br /&gt;
three=3&lt;br /&gt;
four=4&lt;br /&gt;
Using For Loop&lt;br /&gt;
fun main() {&lt;br /&gt;
   val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
   for ((k, v) in theMap) {&lt;br /&gt;
      println(&amp;quot;$k = $v&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
one = 1&lt;br /&gt;
two = 2&lt;br /&gt;
three = 3&lt;br /&gt;
four = 4&lt;br /&gt;
Using forEach&lt;br /&gt;
fun main() {&lt;br /&gt;
   val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
   theMap.forEach { &lt;br /&gt;
      k, v -&amp;gt; println(&amp;quot;Key = $k, Value = $v&amp;quot;) &lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Key = one, Value = 1&lt;br /&gt;
Key = two, Value = 2&lt;br /&gt;
Key = three, Value = 3&lt;br /&gt;
Key = four, Value = 4&lt;br /&gt;
Size of Kotlin Map&lt;br /&gt;
We can use size property or count() method to get the total number of elements in a map:&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
    println(&amp;quot;Size of the Map &amp;quot; + theMap.size)&lt;br /&gt;
    println(&amp;quot;Size of the Map &amp;quot; + theMap.count())&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
Size of the Map 4&lt;br /&gt;
Size of the Map 4&lt;br /&gt;
The containsKey() &amp;amp; containsValue() Methods&lt;br /&gt;
The The containsKey() checks if the map contains a key. The containsValue() checks if the map contains a value.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
   val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
&lt;br /&gt;
   if(theMap.containsKey(&amp;quot;two&amp;quot;)){&lt;br /&gt;
      println(true)&lt;br /&gt;
   }else{&lt;br /&gt;
      println(false)&lt;br /&gt;
   }&lt;br /&gt;
   &lt;br /&gt;
   if(theMap.containsValue(&amp;quot;two&amp;quot;)){&lt;br /&gt;
      println(true)&lt;br /&gt;
   }else{&lt;br /&gt;
      println(false)&lt;br /&gt;
   } &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
true&lt;br /&gt;
false&lt;br /&gt;
The isEmpty() Method&lt;br /&gt;
The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
   val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
   if(theMap.isEmpty()){&lt;br /&gt;
      println(true)&lt;br /&gt;
   }else{&lt;br /&gt;
      println(false)&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
false&lt;br /&gt;
The get() Method&lt;br /&gt;
The get() method can be used to get the value corresponding to the given key. The shorthand [key] syntax is also supported.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
   val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
&lt;br /&gt;
   println(&amp;quot;The value for key two &amp;quot; + theMap.get(&amp;quot;two&amp;quot;))&lt;br /&gt;
   println(&amp;quot;The value for key two &amp;quot; + theMap[&amp;quot;two&amp;quot;])&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
The value for key two 2&lt;br /&gt;
The value for key two 2&lt;br /&gt;
There is also the function getValue() which has slightly different behavior: it throws an exception if the key is not found in the map.&lt;br /&gt;
Map Addition&lt;br /&gt;
We can use + operator to add two or more maps into a single set. This will add second map into first map, discarding the duplicate elements.&lt;br /&gt;
&lt;br /&gt;
If there are duplicate keys in two maps then second map&amp;#039;s key will override the previous map key.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val firstMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3)&lt;br /&gt;
    val secondMap = mapOf(&amp;quot;one&amp;quot; to 10, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    val resultMap = firstMap + secondMap&lt;br /&gt;
    &lt;br /&gt;
    println(resultMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=10, two=2, three=3, four=4}&lt;br /&gt;
Map Subtraction&lt;br /&gt;
We can use - operator to subtract a list from a map. This operation will remove all the keys of the list from the map and will return the result.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3)&lt;br /&gt;
    val theKeyList = listOf(&amp;quot;one&amp;quot;, &amp;quot;four&amp;quot;)&lt;br /&gt;
    val resultMap = theMap - theKeyList&lt;br /&gt;
    &lt;br /&gt;
    println(resultMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{two=2, three=3}&lt;br /&gt;
Removing Entries from Map&lt;br /&gt;
We can use remove() method to remove the element from a mutable map, or we can use minus-assign (-=) operator to perform the same operation&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mutableMapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    theMap.remove( &amp;quot;two&amp;quot;)&lt;br /&gt;
    println(theMap)&lt;br /&gt;
    &lt;br /&gt;
    theMap -= listOf(&amp;quot;three&amp;quot;)&lt;br /&gt;
    println(theMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=1, three=3, four=4}&lt;br /&gt;
{one=1, four=4}&lt;br /&gt;
Sorting Map Elements&lt;br /&gt;
We can use toSortedMap() method to sort the elements in ascending order, or sortedDescending() method to sort the set elements in descending order.&lt;br /&gt;
&lt;br /&gt;
You can also create a sorted map with the given key/values using sortedMapOf() method. Just use this method in place of mapOf().&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    var resultMap = theMap.toSortedMap()&lt;br /&gt;
    println(resultMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{four=4, one=1, three=3, two=2}&lt;br /&gt;
Filtering Map Elements&lt;br /&gt;
We can use either filterKeys() or filterValues() method to filter out the entries.&lt;br /&gt;
&lt;br /&gt;
We can also use filter() method to filter out the elements matching the both key/value&lt;br /&gt;
&lt;br /&gt;
.&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
    val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    var resultMap = theMap.filterValues{ it &amp;gt; 2}&lt;br /&gt;
    println(resultMap)&lt;br /&gt;
    &lt;br /&gt;
    resultMap = theMap.filterKeys{ it == &amp;quot;two&amp;quot;}&lt;br /&gt;
    println(resultMap)&lt;br /&gt;
    &lt;br /&gt;
    resultMap = theMap.filter{ it.key == &amp;quot;two&amp;quot; || it.value == 4}&lt;br /&gt;
    println(resultMap)&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{three=3, four=4}&lt;br /&gt;
{two=2}&lt;br /&gt;
{two=2, four=4}&lt;br /&gt;
Mapping Map Elements&lt;br /&gt;
We can use map() method to map all elements using the provided function:.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
     val theMap = mapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3)&lt;br /&gt;
    val resultMap = theMap.map{ (k, v) -&amp;gt; &amp;quot;Key is $k, Value is $v&amp;quot; }&lt;br /&gt;
    &lt;br /&gt;
    println(resultMap)&lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
[Key is one, Value is 1, Key is two, Value is 2, Key is three, Value is 3]&lt;br /&gt;
Kotlin Mutable Map&lt;br /&gt;
We can create mutable set using mutableMapOf(), later we can use put to add more elements in the same map, and we can use remove() method to remove the elements from the set.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
fun main() {&lt;br /&gt;
     val theMap = mutableMapOf(&amp;quot;one&amp;quot; to 1, &amp;quot;two&amp;quot; to 2, &amp;quot;three&amp;quot; to 3, &amp;quot;four&amp;quot; to 4)&lt;br /&gt;
    &lt;br /&gt;
    theMap.put(&amp;quot;four&amp;quot;, 4)&lt;br /&gt;
    println(theMap)&lt;br /&gt;
    &lt;br /&gt;
    theMap[&amp;quot;five&amp;quot;] = 5&lt;br /&gt;
    println(theMap)&lt;br /&gt;
    &lt;br /&gt;
    theMap.remove(&amp;quot;two&amp;quot;)&lt;br /&gt;
    println(theMap)&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
When you run the above Kotlin program, it will generate the following output:&lt;br /&gt;
&lt;br /&gt;
{one=1, two=2, three=3, four=4}&lt;br /&gt;
{one=1, two=2, three=3, four=4, five=5}&lt;br /&gt;
{one=1, three=3, four=4, five=5}&lt;br /&gt;
Quiz Time (Interview &amp;amp; Exams Preparation)&lt;br /&gt;
Q 1 - Can we make a mutable Kotlin map as immutable?&lt;br /&gt;
&lt;br /&gt;
A - Yes&lt;br /&gt;
&lt;br /&gt;
B - No&lt;br /&gt;
&lt;br /&gt;
Q 2 - We can add two or more maps and create a single set using + operator:&lt;br /&gt;
&lt;br /&gt;
A - True&lt;br /&gt;
&lt;br /&gt;
B - False&lt;br /&gt;
&lt;br /&gt;
Q 2 - Which method will return the value for the given key of Kotlin Map?&lt;br /&gt;
&lt;br /&gt;
A - get()&lt;br /&gt;
&lt;br /&gt;
B - elementAt()&lt;br /&gt;
&lt;br /&gt;
C - Direct index with set variable&lt;br /&gt;
&lt;br /&gt;
D - None of the above&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* https://www.tutorialspoint.com/kotlin/kotlin_maps.htm&lt;/div&gt;</summary>
		<author><name>Unknown user</name></author>
	</entry>
</feed>