Most Used Data Structures in Kotlin(Clash of Data Structures)

Appcaesars
Huawei Developers
Published in
4 min readNov 26, 2021

--

Introduction

In this article , I will be talking about some non primitive data structures that , I use in my applications while developing with Kotlin language.

1.0) One of the fiercest gladiator in the colliesium is MutableList :

1.1) This data structure is mutable(changeable), which can be understandable from it’s name. The methods of MutableList supports both read and write functionalities. Once the elements in MutableList have declared, it can be added more elements in it or removed, so it has no fixed size length.

1.2)Some example for MutableList usage (These are basic usages, there are many more functionality in MutableList interface) :

Variable declaration of MutableList:

Adding(write) item to MutableList:

Removing(write) item from MutableList:

Get(read) item from MutableList:

Set(write) item from MutableList:

Reverse(read) sorting of items in MutableList:

Checking(read) conditions for MutableList:

looping(read) MutableList:

2.0)Second fighter is a disguised one known as ArrayList:

2.1)This data structure is used to create a dynamic Array. In Kotlin ArrayList actually implements to the MutableList. So ArrayList is actually a MutableList in Kotlin. Furthermore previous examples for MutableList applies for ArrayList.

Variable declaration of ArrayList:

etc..

3.0)Third fighter is known for it’s unbreakable behaviour, which is known as List :

3.1)This data structure is a collection of a item similar to MutableList and ArrayList, but it’s immutable(unchangeable).

3.2)Some example for List usage (These are basic usages, there are many more functionality in List interface) and what operations cannot be used at list(mainly writing operations):

Variable declaration of list:

Since List is immutable you cannot use add(write)
You cannot use remove(write)
You cannot set(write) item
You cannot reverse(write) List

Get(read) items from List:

Checking(read) conditions for MutableList:

looping(read) List:

4.0) Fourth fighter is my personal favourite the dark horse of colliesium is HashMap :

4.1)This data structure is a collection which contains pairs of object. It stores the data in the form of key and value pair. Map keys are unique and the map holds only one value for each key. It is represented as HashMap<key, value> or HashMap<K, V>. In addition HashMap isn’t a sequential data structure like the previous examples, so be careful!

4.2)Some example for HashMap usage (These are basic usages, there are many more functionality in HashMap interface) :

Variable declaration of HashMap:

Adding(write) entry to HashMap:

Get(read) entry from HashMap:

Set(write) entry from HashMap:

Checking(read) conditions for HashMap:

looping(read) HashMap:

5.0) Fifth fighter is brother of HashMap, but he sorted his motivation in the colliesium, so he known as LinkedHashMap.

5.1) This data structure is similar to HashMap but it’s a sequential version of HashMap.

Variable declaration of LinkedHashMap:

etc..

Tips & Tricks

  1. For read and write operations use MutableList or ArrayList.(mutable)
  2. For read only operations use List.(immutable)
  3. For double dimension non sequential read and write operations use HashMap.(mutable)
  4. For double dimension sequential read and write operations use LinkedHashMap.(mutable).

Conclusion

In this article I tried to give information about data structures that, I used most frequently in my application development process. I hope it was a useful article for you. Happy coding :)

References

--

--