Kotlin collections for beginners

Kotlin collections for beginners

Most of a programmer's everyday work involves dealing with data. This includes collecting data from the user, manipulating the data, and displaying data to the user.

As a programmer, you will work with different types of data, and most of the time, you will need to work with collections of data.

So what are collections?

If you're familiar with programming then you understand the concept of data types such as boolean, int, string, etc.

Now suppose you are working on a library application that displays and manages books. In this case, you have to use a group of objects (books) of the same type. In common terms, this is a collection because you can collect those objects and add or remove them when needed.

Collections, therefore, contain many objects of the same type.

Collection types in kotlin

Kotlin supports the use of the following types:

  1. List
  2. Sets
  3. Map or Dictionary

You might ask why do you need different types of collections if I will be storing objects of the same type?

So let's explore each of these and understand how different they are from each other.

Lists in kotlin

Lists have the following properties:

  • They are ordered - This means that the order of items in the list matters.

  • They are dynamic- This refers to memory, meaning that the size is not fixed. You can add as many items as you need.

  • They can be immutable or mutable - Mutable lists allow you to add, remove, and insert elements while immutable lists do not allow you to add or remove elements.

  • They can be duplicates- Which means you can have duplicate values or items in the list. For example, you can have two books with the same name in a list of books.

Creating a list

fun main() {
  val list = listOf(2, 3, 5, 6, 7)

The above list contains an immutable list of integer numbers. You cannot insert or remove an element from the above list.

To do that, you need to create a mutable list ie.

fun main() {
  val list = mutablelistOf(2, 3, 5, 6, 7)

Now you can perform operations such as:

  list[2] = 100  // change the element at index 2 to 100
  println(list[2]) // 100
  list.add(index = 3, element = 500) // add an element at index 3
  println(list[3]) // 500
  list.remove(7) // remove element 7
  println(list) // [2, 3, 100, 500, 6]
  list.removeAt(0) // remove the element at index 0 ie element 2
  println(list) // [3, 100, 500, 6]

Sets in kotlin

A set is a collection of elements where each of the elements is unique and there are no duplicates. Properties:

  • They are unordered- order does not matter
  • They are unique - No duplicates
  • They can be either mutable or immutable

Creating sets

To create immutable sets:

fun main() {
  val numbers = setOf(2, 3, 5, 6, 7)

To create mutable sets:

fun main() {
  val numbers = mutableSetOf(2, 3, 5, 6, 7)

Sets use an object’s hashCode() internally to filter out duplicates. If the set is mutable and you try to add another element to it, the new object will replace the old one. You can override hashCode() and manually determine how to differentiate the elements.

Sets are a bit different than lists when it comes to accessing data. You wouldn’t look up items in a set using indices since the index of an item is actually its hash code. If you know the hash code, then you should already have the value.

Maps in kotlin

A Map stores key-value pairs (or entries ); keys are unique, but different keys can be paired with equal values. And like other collections, maps can be both mutable and immutable.

Creating a map

We initialize all the collections the same way. So, just like the other collections, you can create mutable or immutable maps:

Immutable map

fun main() {
  val peopleAges = mapOf(
      "Fred" to "23",
      "Ann" to "9",
      "Grace" to "40")

Mutable map

fun main() {
  val peopleAges = mutableMapOf(
      "Fred" to "23",
      "Ann" to "9",
      "Grace" to "40")

With mutable maps, you can add or remove items using the keys and the method put().

Add items

peopleAges.put("Nick", 22) //This adds another key-value element

Change items

peopleAges["Fred"]=24  //This changes the value of Fred's age from 23 to 24

Iterating through maps is a bit different from the rest of the Kotlin collections. Since maps have two objects for each element, you have to iterate over pairs, instead of single values. You can do this in the following way:

peopleAges.forEach { key, value -> println("Value for key $key is $value") }

You iterate through the pairs. In each iteration, you receive both the key and the value for that key, since it’s often necessary to know and consume both the key and the value.


Collections are very crucial in programming. This tutorial has introduced you to the most common collections in kotlin. To read more about kotlin collections and the various methods associated with each collection type, check out the Kotlin documentation.