DEV Community

Magda Miu
Magda Miu

Posted on • Originally published at Medium on


Collections in Kotlin

It’s time to continue our learning path in Kotlin. The subject covered in this new post is represented by Collections and data operations applied to them.

Collections are actually a set of classes and interfaces which provide high quality implementations of useful data structures and algorithms that help developers to reduce the programming effort and time.


In Kotlin there are different flavors of collections. Just like Java, all collection interfaces are originated from the Iterable interface. The main difference between Java and Kotlin is that in Kotlin we have mutable and immutable collections. An important thing to mention here is that immutable collections in Kotlin are simply read-only.

So in terms of collections these are the main ones:

  1. Pair  — a tuple of two values and Triple  — a tuple of three values.
  2. Array  — indexed fixed-sized collection of objects and primitives.
  3. List  — ordered collection of objects.
  4. Set  — unordered collection of objects.
  5. Map  — associative dictionary or map of keys and values.

1️⃣ Pair and Triple

Are very helpful if we want to quickly create two (Pair) or three objects (Triple) as a collection


📝Class definition

data class Pair<out A, out B> : Serializable

👩‍💻Code examples


📝Class definition

data class Triple<out A, out B, out C> : Serializable

👩‍💻Code examples

2️⃣ Array

A block memory that keeps multiple values in a sequence; very helpful for low-level optimizations.


📝Class definition

class Array<T>

👩‍💻Code examples

Both the immutableArray and mutableArray arrays are fixed in size, but the elements of each array are mutable and can be updated. The only difference is that immutableArray is declared with the val keyword, so this array cannot be reassigned.

3️⃣ List

Are data structures that hold a number of items in a sequence. Kotlin provides two different types of lists:

  • immutable lists (does not have add function)
  • mutable lists


📝Class definition

interface List<out E> :[Collection](<E>


📝Class definition

interface MutableList<E> : [List](<E>, [MutableCollection](<E>

👩‍💻Code examples

4️⃣ Set

Are unordered collections of elements. Other than using setOf() / Set and mutableSetOf() / MutableSet we could also use hashSetOf() / java.util.HashSet


📝Class definition

interface Set<out E> : [Collection](<E>


📝Class definition

interface MutableSet<E> : [Set](<E>, [MutableCollection](<E>

👩‍💻Code examples

5️⃣ Map

Keeps a collection of key-value pairs.


📝Class definition

interface Map<K, out V>


📝Class definition

interface MutableMap<K, V> : [Map](<K, V>

👩‍💻Code examples

🕵️‍♀️Data operations in a collection

📌Input data

📌 Aggregate functions

📌 Search functions

📌 Filter functions

📌 Transform functions

Check my previous articles about Kotlin:

Enjoy and feel free to leave a comment if something is not clear or if you have questions. And if you like it please share!

Thank you for reading! 🙌🙏😍✌

Follow me on:

Originally published at on March 14, 2020.

Top comments (0)

50 CLI Tools You Can't Live Without

>> Check out this classic DEV post <<