Collections in Kotlin
Tags: collection, kotlin, learnkotlinfromgde, androiddev
Published: 2020-06-27 13:23:38

---
title: Collections in Kotlin
published: true
date: 2020-03-14 14:48:50 UTC
tags: collection,kotlin,learnkotlinfromgde,androiddev
canonical_url: https://medium.com/@magdamiu/collections-in-kotlin-a2bd8649f697
---

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.

![](https://cdn-images-1.medium.com/max/640/0*EycoPpnpbg31EbVD)

### 🕵️‍♀️Collections

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**.

![](https://cdn-images-1.medium.com/max/640/0*Lpp0--c1ymMIsQXv)

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_

### 🔗[Pair](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-pair/)

#### 📝Class definition

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

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/a3cbbd009961e310a27f3f42055a3201 %}

### 🔗[Triple](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-triple/)

#### 📝Class definition

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

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/c48baea400139fe96d70b8ee41f81dee %}

### 2️⃣ Array

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

![](https://cdn-images-1.medium.com/max/640/0*bJ52DJ1UJGv9UsSR)

### 🔗[Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/)

#### 📝Class definition

```
class Array<T>
```

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/e2dd0c7d5cf49ac796fd79c53d8ebd61 %}

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

### 🔗[List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/)

#### 📝Class definition

```
interface List<out E> :[Collection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html)<E>
```

### 🔗[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/)

#### 📝Class definition

```
interface MutableList<E> : [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<E>, [MutableCollection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-collection/index.html)<E>
```

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/cd055fab88ff45d5787ba7425f67032d %}

### 4️⃣ Set

Are unordered collections of elements. Other than using _setOf() / Set<T>_ and _mutableSetOf() / MutableSet<T>_ we could also use _hashSetOf() / java.util.HashSet<T>_

### 🔗[Set](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/)

#### 📝Class definition

```
interface Set<out E> : [Collection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html)<E>
```

### 🔗[MutableSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/)

#### 📝Class definition

```
interface MutableSet<E> : [Set](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html)<E>, [MutableCollection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-collection/index.html)<E>
```

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/7fb60c1b7cfa4f4e3008088a355e7e23 %}

### 5️⃣ Map

Keeps a collection of key-value pairs.

### 🔗[Map](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/)

#### 📝Class definition

```
interface Map<K, out V>
```

### 🔗[MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/#kotlin.collections.MutableMap)

#### 📝Class definition

```
interface MutableMap<K, V> : [Map](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html)<K, V>
```

#### 👩‍💻Code examples

{% gist https://gist.github.com/magdamiu/d6567474f8e79de03fe4ed64f641e0ed %}

### 🕵️‍♀️Data operations in a collection

### 📌Input data

{% gist https://gist.github.com/magdamiu/5673bfa48185fa2fe357f89ce1105e6b %}

### 📌 Aggregate functions

{% gist https://gist.github.com/magdamiu/ebe171babd586ab225a090f3ac2a0558 %}

### 📌 Search functions

{% gist https://gist.github.com/magdamiu/e96b835637323ab061bbd67006acb732 %}

### 📌 Filter functions

{% gist https://gist.github.com/magdamiu/2f55b505ea28c13ea268a5a36efe9141 %}

### 📌 Transform functions

{% gist https://gist.github.com/magdamiu/5b20f9b13a16233dc6763e100285a713 %}

Check my previous articles about Kotlin:

- [Hello Kotlin](https://medium.com/@magdamiu/hello-kotlin-774b44cd9df0)
- [Kotlin Basics](https://dev.to/magdamiu/kotlin-basics-ln0)
- [Kotlin functions are fun](https://proandroiddev.com/kotlin-functions-are-fun-8fdcd4e85a5)
- [Classes in Kotlin](https://proandroiddev.com/classes-in-kotlin-9cf584556431)

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:

- [Magda Miu](https://twitter.com/magdamiu)
- [Magda Miu - DEV Profile](https://dev.to/magdamiu)

_Originally published at_ [_http://magdamiu.com_](https://magdamiu.com/2020/03/14/collections-in-kotlin/) _on March 14, 2020._