Android Programming – Part 6 Kotlin Fundamentals Ⅵ: Sets and Maps Collections

Sets and Maps!

Okay, this is another post for Kotlin’s collections. This time, we’ll learn about the basics of sets and maps. Since both are one of the core concepts of the language, you need to master them before actually starting coding in Android!

This time, let’s learn set and map. While set is a collection of unique elements, map is Kotlin’s version of a dictionary. Let’s dive into it!

Set is a collection of unique elements. It reflects the mathematical abstraction of set: a group of objects without repetitions. Generally, the order of set elements has no significance. For example, the numbers on lottery tickets form a set: they are unique, and their order is not important.

kotlinlang.org

MySetSample.kt

fun listOfMySet(set: Set<Any>) {
    for (country in set) {
        println(country)
    }
}

fun main() {
    val mySet = setOf("US", "UK", "AUS")
    listOfMySet(mySet)

}
$java -jar MySetSample.jar
US
UK
AUS

setOf is immutable:

This is something you have to keep in mind that setOf is immutable, so just like the code below, you can’t “add” a new element.

MySetSample2.kt

fun listOfMyMutableSet(set: Set<Any>) {
    for (country in set) {
        println(country)
    }
}

fun main() {
    val mySet = setOf("US", "UK", "AUS")
    mySet.add("AUS")
    listOfMyMutableSet(mySet)
}
$python3 kotlinc.py
What's the file name? MySetSample2
MySetSample2.kt:9:11: error: unresolved reference: add
    mySet.add("AUS")

So, in order to add a new element, you have to create a new set and add a new element to it.

fun listOfMyMutableSet(set: Set<Any>) {
    for (country in set) {
        println(country)
    }
}

fun main() {
    val mySet = setOf("US", "UK", "AUS")
    val newMySet = mySet + "AUS"
    listOfMyMutableSet(newMySet)
}

It worked, but the newly added AUS is ignored. So, here is an additional thing you have to be aware of. As it’s described by Kotrlin’s official page, which I mentioned earlier in this post, set is a collection of unique elements, so none of it can be duplicated. So the newly added AUS is ignored this time.

johnito@skynewubuntuserver:testdir$ java -jar MySetSample2.jar
US
UK
AUS

Contrary, listOf() has no problem with element duplication. Here’s an example.

fun listOfMyMutableSet(set: MutableList<String>) {
    for (country in set) {
        println(country)
    }
}

fun main() {
    val mySet = mutableListOf("US", "UK", "AUS")
    mySet.add("AUS")
    listOfMyMutableSet(mySet)
}

AUS is duplicated but NOT ignored.

$java -jar MySetSample3.jar
US
UK
AUS
AUS

mapOf()

Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value. The values can be duplicates. Maps are useful for storing logical connections between objects, for example, an employee’s ID and their position.

kotlinlang.org

MyMapSample.kt

fun listOfMyMap(map: Map<String,Int>) {
    for (country in map) {
        println(country)
    }
}

fun main() {
    val myMap = mapOf("Up" to 1, "Down" to 2, "Left" to 3, "Right" to 4)
    listOfMyMap(myMap)
}
$java -jar MyMapSample.jar
Up=1
Down=2
Left=3
Right=4

mutableMapOf()

MyMutableMapSample.kt

fun listOfMyMap(map: MutableMap<String,Int>) {
    for (country in map) {
        println(country)
    }
}

fun main() {
    val myMutableMap = mutableMapOf("Up" to 1, "Down" to 2, "Left" to 3, "Right" to 4)
    listOfMyMap(myMutableMap)
}
$java -jar MyMutableMapSample.jar
Up=1
Down=2
Left=3
Right=4

put()

MyMutableMapSample2.kt

fun listOfMyMap(map: MutableMap<String,Int>) {
    for (num in map) {
        println(num)
    }
}

fun main() {
    val myMutableMap = mutableMapOf<String, Int>()
    myMutableMap.put("One", 1)
    myMutableMap.put("Two", 2)
    myMutableMap.put("Three", 3)
    listOfMyMap(myMutableMap)
}
$java -jar MyMutableMapSample2.jar
One=1
Two=2
Three=3

via:

kotlinlang.org

Leave a Reply