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.

🕵️‍♀️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.

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

📝Class definition

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

👩‍💻Code examples

🔗Triple

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

🔗Array

📝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

🔗List

📝Class definition

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

🔗MutableList

📝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

4️⃣ Set

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

🔗Set

📝Class definition

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

🔗MutableSet

📝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

5️⃣ Map

Keeps a collection of key-value pairs.

🔗Map

📝Class definition

interface Map<K, out V>

🔗MutableMap

📝Class definition

interface MutableMap<K, V> : [Map](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html)<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 http://magdamiu.com on March 14, 2020.

Discussion (0)