loading...
Kotlin

Kotlin Collections - Introduction (part-1)

Kotlin Collections Overview

Kotlin collections are part of the Kotlin Standard Library. It provides to set of tools for managing collections - groups of a variable number of items.

Basically, Collections are the common features of any programming language. Collections contain a number of objects of the same type. Objects in a collection are known as elements or items.

Below are the collection types are relevant for Kotlin:

1. List: An ordered collection that can be accessed by indices - that indicate object position. The objects may be repeated in the collection.

2. Set: is a collection of unique elements. It reflects the mathematical abstraction of set a group of objects without repetitions.

3. Map: a set of key-value pairs. Keys are unique, and each of the maps to exactly one value. The values can be duplicated. Maps are useful for storing logical connections between objects, for example, an employee’s ID and their position.

The collection interfaces and related functions are located in the kotlin.collections package.

As we have discussed, the Kotlin Standard Library provides implementations for basic collection types: sets, lists, and maps.

Collections interfaces represent each collection type:

Read-only: This type of collection are basically for accessing collections elements. Which can’t be modified.

mutable: This type of collection can provide access to the collection by adding, removing, and updating its elements.

Modifying a mutable collection doesn’t require it to be a var: write operations modify the same mutable collection object, so the reference doesn’t change. Although, if you try to reassign a val collection.
you’ll get a compilation error:

fun main() {
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five") // this is OK
numbers = mutableListOf("six", "seven") // compilation error
}

Now let’s take a look at and discuss the Kotlin standard library collections:
Collection:

1. Collection

This interface represents a read-only collection. It is the root of the collection hierarchy. Collection inherits from the Iterable<T>, interface that defines the operations for iterating elements.
Taking Collection as a param which can be applied to different collection types like List and Set.

fun printAllElements(strings: Collection<String>) {
        for(item in strings) print("$item ")
        println()
    }
    
fun main() {
    val stringList = listOf("first", "second", "first")
    printAllElements(stringList)
    
    val stringSet = setOf("first", "second", "third")
    printAllElements(stringSet)
}

2. MutableCollection
It is a Collection with write operations, such as add and removes.

fun List<String>.getShortWordsTo(shortWords: MutableList<String>, maxLength: Int) {
    this.filterTo(shortWords) { it.length <= maxLength}
    // throwing away the articles
    val articles = setOf("a", "A", "an", "An", "the", "The")
    shortWords -= articles
}

fun main() {
    val words = "A long time ago in a galaxy far far away".split(" ")
    val shortWords = mutableListOf<String>()
    words.getShortWordsTo(shortWords, 3)
    println(shortWords)
    words.getShortWordsTo(shortWords, 2)
    println(shortWords)
}

Wrap Up!

So, that was a pretty cool introduction to Kotlin collections. We will discuss more collection in the next part of this series.

🙏 Thanks for reading this article. Don’t forget to share as much as you can with your friends. It means a lot to me.
For more about programming, follow me, so you’ll get notified whenever a new article is posted.

Close Bitnami banner
Bitnami