Collections in Kotlin

5 min
Collections in Kotlin

I can confidently say, that most applications could not work without data collections. Those type of concept was created to hold specific values for later usage. Similar to Java Collections, Kotlin also provides a rich set of tools for managing collections.

A collection usually contains a number of objects of the same type and these objects in the collection are called elements or items. In Kotlin collections are categorized in two forms: Immutable and mutable collections.

Some basic information about the immutability and mutability you can find in the post about Variables in Kotlin. In a nutshell, immutable means that a collection or a variable supports only read-only functionalities. So once defined values cannot be reassigned. Mutable collections are the opposite, so any item‘s value can be changed.

Immutable Collections

List – listOf() and listOf<T>()

It is an ordered collection in which we can access to each element by indices. Indice typically is an integer number that defines an element’s position. Elements can be repeated in a list in anytime.

val immutableList = listOf("Cezary", "Katarzyna", "Aureliusz")
for (element in immutableList) {
    println(element)
}

val secondImmutableList = listOf<Int>(1, 2, 3, 4, 5)
for (number in secondImmutableList) {
    println(number)
}

Set

Is an unordered collection of elements without duplicates, so we cannot add two or more elements with the same value. Simply, it is a collection of unique elements and order of elements is not significant. We cannot perform any add or remove operations, because it’s immutable collection.

val immutableSet = setOf("Cezary", "Katarzyna", 1, 2, 3, 4, 0, 0, 0)
for (item in immutableSet) {
    println(item)
}

Map

Map is a collection of unique keys which each holds one value, to it is a set of key-value pairs. Each key maps to exacly one value. The values can be duplicated, but keys must be unique. Maps are used to store logical condition between two objects, for example – a client ID and his name.

val immutableMap = mapOf(1 to "Cezary", 2 to "Katarzyna")
for (key in immutableMap.keys) {
    println(immutableMap[key])
}

Mutable Collections

List – mutableListOf(), arrayListOf() and ArrayList

Mutable lists support read and write operations, assigned values may be changed or removed.

val mutableList = arrayListOf("Tomasz", "Katarzyna")
mutableList[0] = "Cezary"
mutableList.add("Aleksandra")
mutableList.add("Krzysztof")
mutableList.removeAt(3)
for (element in mutableList) {
    println(element)
}

val secondMutableList = arrayListOf<Int>()
secondMutableList.add(1)
secondMutableList.add(2)
secondMutableList.add(3)
for (element in secondMutableList) {
    println(element)
}

Set – mutableSetOf(), hashSetOf()

Also supports read and write operations. We can easily add new or remove old element with a preservation of order.

val mutableSet = hashSetOf(1, 10, 11)
mutableSet.add(2)
mutableSet.add(3)
mutableSet.add(1)
for (element in mutableSet) {
    println(element)
}

Map – mutableMapOf(), hashMapOf() and HashMap

As previously wrote about an immutable mp, the main difference here is that mutable map supports read and write operations.

val mutableMap = mutableMapOf<String, String>("ID" to "1", "name" to "Cezary")
mutableMap["email"] = "cezary.kaszuba@sunnylib.com"
mutableMap.put("sex", "male")
for (key in mutableMap.keys) {
    println("$key: " + mutableMap[key])
}
println("~~~~~~~~~~")
for (value in mutableMap.values) {
    println(value)
}

Feel free to leave a comment blow, best regards!


Post a Comment

Your email address will not be published. Required fields are marked *