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 that provides high quality implementations of useful data structures and algorithms that help developers to reduce the programming effort and time.

collections kotlin magda miu

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

collections framework in kotlin

So in terms of collections these are the main ones:

  1. Paira tuple of two values and Triplea tuple of three values.
  2. Arrayindexed fixed-sized collection of objects and primitives.
  3. Listordered collection of objects.
  4. Setunordered collection of objects.
  5. Mapassociative dictionary or map of keys and values.

 

1️⃣ Pair and Triple

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

🔗Pair 

📝Class definition

data class Pair<out Aout B> : Serializable

👩‍💻Code examples

🔗Triple

📝Class definition

data class Triple<out Aout Bout C> : Serializable

👩‍💻Code examples

2️⃣ Array

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

array in kotlin

🔗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

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<E>

🔗MutableList

📝Class definition

interface MutableList<E> : List<E>MutableCollection<E>

👩‍💻Code examples

4️⃣ Set

Unordered collections of elements. Other than using setOf() / Set<T> and mutableSetOf() / MutableSet<T> we could also use hashSetOf() / java.util.HashSet<T> 

🔗Set

📝Class definition

interface Set<out E> : Collection<E>

🔗MutableSet

📝Class definition

interface MutableSet<E> : Set<E>MutableCollection<E>

👩‍💻Code examples

5️⃣ Map

Keeps a collection of key-value pairs.

🔗Map

📝Class definition

interface Map<Kout V>

🔗MutableMap

📝Class definition

interface MutableMap<KV> : Map<K, V>

👩‍💻Code examples

🕵️‍♀️Data operations in a collection

📌Input data

📌 Aggregate functions

📌 Search functions

📌 Filter functions

📌 Transform functions

Check here 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:

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s