Maps (Collections) in kotlin
package com.example.my_kotlin_practice
fun main(args: Array<String>)
{
// 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.
// A Kotlin map can be either mutable (mutableMapOf) or read-only (mapOf).
// Maps are also known as dictionaries or associative arrays in other programming languages
val theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMap)
val theMutableMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(theMutableMap)
// Creating Map using HashMap : A Kotlin map can be created from Java's HashMap.
val theMap2 = HashMap<String, Int>()
theMap2["one"] = 1
theMap2["two"] = 2
theMap2["three"] = 3
theMap2["four"] = 4
println("Creating Map using HashMap : $theMap2")
// Using Pair while Creating Map : We can use Pair() method to create key/value pairs
val theMap3 = mapOf(Pair("one",1),Pair("two",2),Pair("three",3),Pair("four",4))
println("Using Pair while Creating Map : $theMap3")
// Kotlin Properties : Kotlin map has properties to get all entries, keys, and values of the map
val theMap4 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Entries : ${theMap4.entries}")
println("Keys : ${theMap4.keys}")
println("Values : ${theMap4.values}")
// Loop through Kotlin Maps
val theMap5 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Loop through Kotlin Maps : ${theMap5.toString()}")
// Using Iterator
println("Below is using 'Iterator' example : ")
val theMap6 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
val itr = theMap6.keys.iterator()
while(itr.hasNext()) {
val key = itr.next()
val value = theMap6[key]
println("${key}=${value}")
}
// Using For Loop
println("Below is 'Using For Loop' example : ")
val theMap7 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
for((k,v) in theMap7) {
println("$k = $v")
}
// Using forEach
println("Below is 'Using forEach' example : ")
val theMap8 = mapOf("six" to 6, "seven" to 7, "eight" to 8, "nine" to 9, "ten" to 10)
theMap8.forEach { (k,v) -> println("Key = $k, Value = $v") }
// Size of Kotlin Map : We can use size property or count() method to get the total number of elements in a map
val theMap9 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Size of the Map (using 'size' property) : ${theMap9.size}")
println("Size of the Map (using 'count()' method) : ${theMap9.count()}")
// The containsKey() & containsValue() Methods : The The containsKey() checks if the map contains a key.
// The containsValue() checks if the map contains a value
println("Below is 'The containsKey() & containsValue() Methods' example : ")
val theMap10 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
if(theMap10.containsKey("two")) {
println("Yes, given key contains in above given map : ${true}")
} else { println("No, given key not contains in above given map : ${false}") }
if(theMap10.containsValue(2)) {
println("Yes, given value contains in above given map : ${true}")
} else { println("No, given value not contains in above given map : ${false}") }
if(theMap10.containsKey("six")) {
println("Yes, given key contains in above given map : ${true}")
} else { println("No, given key not contains in above given map : ${false}") }
if(theMap10.containsValue(6)) {
println("Yes, given value contains in above given map : ${true}")
} else { println("No, given value not contains in above given map : ${false}") }
// The isEmpty() Method : The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise
println("Below is 'The isEmpty() Method' example : ")
val theMap11 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
if(theMap11.isEmpty()) {
println("Yes, given map is empty : ${true}")
} else { println("No, given map is not empty : ${false}") }
// The get() Method : The get() method can be used to get the value corresponding to the given key.
// The shorthand [key] syntax is also supported
println("Below is 'The get() Method' example : ")
val theMap12 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("The 'value' for the key 'two' is : ${theMap12.get("two")}")
println("The 'value' for the key 'two' is : ${theMap12["two"]}")
// Map Addition : 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.
//If there are duplicate keys in two maps then second map's key will override the previous map key
println("Below is 'Map Addition' example : ")
val firstMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
val secondMap = mapOf("one" to 10, "four" to 4, "five" to 5, "six" to 6)
val resultMap = firstMap + secondMap
println("Map Addition : $resultMap")
// Map Subtraction : 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
println("Below is 'Map Subtraction' example : ")
val theMap13 = mapOf("one" to 1, "two" to 2, "three" to 3)
val theKeyList = listOf("one","four")
val resultMap2 = theMap13 - theKeyList
println("Map Subtraction : $resultMap2")
// Removing Entries from Map : 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
println("Below is 'Removing Entries from Map' example : ")
val theMap14 = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5, "six" to 6)
theMap14.remove("two")
println("Removing Entries from Map : $theMap14")
theMap14 -= listOf("three")
println("Removing Entries from Map : $theMap14")
// Sorting Map Elements : We can use toSortedMap() method to sort the elements in ascending order, or
// sortedDescending() method to sort the set elements in descending order.
// You can also create a sorted map with the given key/values using sortedMapOf() method.
// Just use this method in place of mapOf().
println("Below is 'Sorting Map Elements' example : ")
val theMap15 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Sorting Map Elements : ${theMap15.toSortedMap()}")
val theMap16 = sortedMapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Sorted Map using 'sortedMapOf' : $theMap16")
// Filtering Map Elements : We can use either filterKeys() or filterValues() method to filter out the entries.
//We can also use filter() method to filter out the elements matching the both key/value
println("Below is 'Filtering Map Elements' example : ")
val theMap17 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
println("Filtering Map Elements (filterValues): ${theMap17.filterValues{ it > 2 }}")
println("Filtering Map Elements (filterKeys) : ${theMap17.filterKeys{ it == "two" }}")
println("Filtering Map Elements (filter) : ${theMap17.filter{ it.key == "two" || it.value == 4 }}")
// Mapping Map Elements : We can use map() method to map all elements using the provided function
println("Below is 'Mapping Map Elements' example : ")
val theMap18 = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
theMap18.map { (k,v) -> println("Key is $k, Value is $v") }
val resultMap3 = theMap18.map { (k,v) -> "Key is $k, Value is $v" }
println("Mapping Map Elements : $resultMap3")
// Kotlin Mutable Map : 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
println("Below is 'Kotlin Mutable Map' example : ")
val theMap19 = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4, "five" to 5)
theMap19.put("six",6)
println("Kotlin Mutable Map : $theMap19")
theMap19["seven"] = 7
println(theMap19)
theMap19.remove("two")
println("Kotlin Mutable Map (remove) : $theMap19")
}
OUTPUT :
{one=1, two=2, three=3, four=4} {one=1, two=2, three=3, four=4} Creating Map using HashMap : {four=4, one=1, two=2, three=3} Using Pair while Creating Map : {one=1, two=2, three=3, four=4} Entries : [one=1, two=2, three=3, four=4, five=5] Keys : [one, two, three, four, five] Values : [1, 2, 3, 4, 5] Loop through Kotlin Maps : {one=1, two=2, three=3, four=4, five=5} Below is using 'Iterator' example : one=1 two=2 three=3 four=4 five=5 Below is 'Using For Loop' example : one = 1 two = 2 three = 3 four = 4 five = 5 Below is 'Using forEach' example : Key = six, Value = 6 Key = seven, Value = 7 Key = eight, Value = 8 Key = nine, Value = 9 Key = ten, Value = 10 Size of the Map (using 'size' property) : 5 Size of the Map (using 'count()' method) : 5 Below is 'The containsKey() & containsValue() Methods' example : Yes, given key contains in above given map : true Yes, given value contains in above given map : true No, given key not contains in above given map : false No, given value not contains in above given map : false Below is 'The isEmpty() Method' example : No, given map is not empty : false Below is 'The get() Method' example : The 'value' for the key 'two' is : 2 The 'value' for the key 'two' is : 2 Below is 'Map Addition' example : Map Addition : {one=10, two=2, three=3, four=4, five=5, six=6} Below is 'Map Subtraction' example : Map Subtraction : {two=2, three=3} Below is 'Removing Entries from Map' example : Removing Entries from Map : {one=1, three=3, four=4, five=5, six=6} Removing Entries from Map : {one=1, four=4, five=5, six=6} Below is 'Sorting Map Elements' example : Sorting Map Elements : {five=5, four=4, one=1, three=3, two=2} Sorted Map using 'sortedMapOf' : {five=5, four=4, one=1, three=3, two=2} Below is 'Filtering Map Elements' example : Filtering Map Elements (filterValues): {three=3, four=4, five=5} Filtering Map Elements (filterKeys) : {two=2} Filtering Map Elements (filter) : {two=2, four=4} Below is 'Mapping Map Elements' example : Key is one, Value is 1 Key is two, Value is 2 Key is three, Value is 3 Key is four, Value is 4 Key is five, Value is 5 Mapping Map Elements : [Key is one, Value is 1, Key is two, Value is 2, Key is three, Value is 3, Key is four, Value is 4, Key is five, Value is 5] Below is 'Kotlin Mutable Map' example : Kotlin Mutable Map : {one=1, two=2, three=3, four=4, five=5, six=6} {one=1, two=2, three=3, four=4, five=5, six=6, seven=7} Kotlin Mutable Map (remove) : {one=1, three=3, four=4, five=5, six=6, seven=7} Process finished with exit code 0
Comments
Post a Comment