According to the Oxford dictionary, a collection is, in the simplest definition I have ever seen, a group of things or people. But, to be honest, there’s no reason to complicate this. You can have a collection of stamps, rare coins, some people collect beer bottle caps, some people collect Funko Pops, some people collect dead insects. The truth is, you can collect pretty much anything as it’s just a group of things (or people ¯\_(ツ)_/¯ ).

In programming, a collection is not different. It’s a group of related items. It can have items that are ordered or unordered, and they can be unique or have duplicates. Collections can also be mutable or immutable. So flexible!! 😉
Lists
Immutable -> listOf() and listOf<T>()
Mutable -> mutableListOf(), arrayListOf() and ArrayList
The first type of collection I’ll talk about is a list. Lists hold an ordered collection of values and they allow duplicates. So you can have a list that holds integers 1, 2, 3, 3, 3, 5, 7
for example. Kotlin distinguishes lists between mutable and immutable and provides several functions for manipulating them.
You can define a list as follows:
val myNumbers = listOf( 0, 3, 4, 5, 3, 3, 6, 8, 9, 7, 7)
📌 Worth noting: listOf returns a read-only (immutable) ordered list of items. However, when I say ordered, I don’t mean sorted. Ordered here means that you can access a specific element of the list that is in a specific position because they have an order. If you type:
println(myNumbers[2])
the output of this code in the console will be the number 8. Ordered in this case means you have access to each element by its index (position).
If you have two lists containing the same elements, but they have a different order, the lists are not the same.
So:
val numbers = listOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2)
val numbers2 = listOf(0, 8, 3, 4, 0, 5, 5, 8, 9, 2)
Note that elements 3 and 8 are in different positions. If you type println(numbers == numbers2)
the output will be false.
While if you compare:
val numbers = listOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2)
val numbers2 = listOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2)
the output will be True.
Remember I mentioned Kotlin provides several functions so you can manipulate collections? Of course you do, I said that 20 lines above. So, if you want your list to be sorted, for example, you can do that. You can call a function called, well, sorted()
on your list and it will return a copy of the list sorted in ascending order.
In this case, we would have:
val numbers = listOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2) println(numbers.sorted())
and the output would be:
[0, 0, 2, 3, 4, 5, 5, 8, 8, 9]
Lists are like phone numbers. You can have the same digit more than once, like 555-3224, but change the order of the numbers when you dial and you will call a different person.
🧨🧨🧨🧨
Be careful!!! What happens if you try to access an element by its index but the index does not exist? For example, you access the fourth item in a list that contains only three?
You will get an ArrayIndexOutOfBoundsException exception.
But Kotlin has your back. You can use some alternatives that are safer. You can use for example numbers.last()
to access the last index of your list if you are not sure how many items it contains. You can also specify an action to be taken instead of throwing an exception using getOrElse. It takes two arguments, the requested index and a lambda that generates a default value.
So you can have numbers.getOrElse(10) {"Index Unknown"}
Another option is using getOrNull
which returns null instead of the exception.
Sets
Another type of collection in Kotlin is called a Set. This is a group of related items, but, unlike a list, the set does not allow duplicates and the items are not ordered. Sets are like lottery numbers: the order the numbers are drawn does not make a difference, but there can be only one number of each.
This is similar to the concept of sets in math. I can have a set of books I’ve read, but the fact that I have read the same books several times does not change the fact that there is only one of each book.

So, if you take the same numbers we used above and create a set, some magic happens:
val numbers = setOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2)
println(numbers)
The output of this code will be:
[0, 3, 8, 4, 5, 9, 2] – Note all duplicates have been removed.
Also, let’s create two different sets with the same numbers but in a different order (we’ll keep it simple to make it easier to read) and then compare both:
val mySet1 = setOf(1,2,3)
val mySet2 = mutableSetOf(3,2,1)
println("$mySet1 == $mySet2: ${mySet1 == mySet2}")
The output is:
[1, 2, 3] == [3, 2, 1]: true
They don’t care about order. It doesn’t even care if one is mutable and the other isn’t.
And just like with lists, you can perform operations using functions provided by the Kotlin library. You can, for example, check if an element is part of a set using contain()
or find the intersection (∩) between two sets, or even the union (∪) of two sets, just like in math 💛
Maps

Hummmm, not exactly this one.
A map in Kotlin (not only in Kotlin, but that’s what we are talking about here) is a set of pairs containing a key and a correspondent value. They are designed to make it easier to look up a value given a particular key. Keys are unique and each key maps to exactly one value but the values can have duplicates. Values in a map can be Strings, Numbers, custom objects or even another collection, like a list.
So you can have the key Dolphins —> [Amazon river dolphin, Baiji, Striped dolphin, Orca]
And NO, orcas are not whales. They are a type of dolphin.
A map is useful when you have pairs of data. For example students to their grades:
val studentGrade = mutableMapOf (
"Allan" to 9,
"Brad" to 8,
"Julia" to 10,
)
println(studentGrade)
And the output is:
{Allan=9, Brad=8, Julia=10}
To add more entries to the map, you can use the put()
function, passing in the key and the value:
studentGrade.put("Carol", 8)
And if you print the map again you will have {Allan=9, Brad=8, Julia=10, Carol=8}
As I mentioned above, the keys need to be unique, but as you can see, the values can be duplicates and we have Carol and Brad with the same grade 8.