Amazing! It’s really nice that you have learned the basics of Kotlin and are comfortable with them. Now, its time to explore deeper topics and help you build strong knowledge of concepts that are actually used in Software Industry frequently. Welcome to Collections!

According to kotlinlang.org, a collection usually contains a number of objects (this number may also be zero) of the same type. Objects in a collection are called elements or items. For example, all the employees in a department form a collection that can be used to calculate their average age.

So, there are basically 3 types of collections in Kotlin as follows:

 

  • List, which is an ordered collection with access to elements by indices – integer numbers that reflect their position. In a list, elements can occur more than once.
  • Set, which is a collection of unique elements. It reflects the mathematical abstraction of the set: a group of objects without repetitions. The set of elements has no significance.
  • Map (or dictionary), which is a set of key-value pairs. Keys are unique, while each of them, maps to exactly one value. Although, the values can be duplicates. Maps are useful for storing logical connections between objects, for example, an employee’s ID and their position.

 

Kotlin – Arrays

In Kotlin, Array has mutable nature with a fixed size. we can perform both read and write operations on Array elements.

Array syntax:

var firstArray = Array<Int>(4){0}

Array declaration:

var firstArray1 = arrayOf(1,2,3,4,5)  
var firstArray2 = arrayOf<Int>(1,2,3,4,5)  
val secondArray = arrayOf<String>("Suresh","Mahesh","Rambabu","Jaiswal","Mihir")  
var thirdArray  = arrayOf(1,2,3, "Ram","Lal")  

Simple example:

 fun main(args: Array<String>){  
    val name = arrayOf<String>("Suresh","Mahesh","Rambabu","Jaiswal","Mihir")  
    var myArray2 = arrayOf<Int>(1,2,3,4,5)  
    var myArray3 = arrayOf(1,2,3,4,5)  
    var myArray4= arrayOf(1,2,3, "Ram","Lal")  
    var myArray5: IntArray = intArrayOf(5,10,15,20,25)  
  
    for(element in name){  
        println(element)  
    }  
  
    println()  
    for(element in myArray2){  
        println(element)  
    }  
    println()  
    for(element in myArray3){  
        println(element)  
    }  
    println()  
    for(element in myArray4){  
        println(element)  
    }  
    println()  
    for(element in myArray5){  
        println(element)  
    }  
  }

//Output:
Suresh
Mahesh
Rambabu
Jaiswal
Mihir

1
2
3
4
5

1
2
3
4
5

1
2
3
Ram
Lal

5
10
15
20
25

Kotlin – Collections

Generally, by using Collection, we can store, retrieve manipulate, and aggregate data.

Collections in Kotlin are broadly categorized into two different forms. These are:

  1. Immutable Collection (or Collection)
  2. Mutable Collection

Now let’s look into Immutable Collection.m

Immutable Collection

Simply known as Collection. They support read-only functionalities. Let’s look at the methods of Immutable collections :

List: listOf(), listOf<T>()

Map: mapOf()

Set: setOf()

Mutable Collection

They support both read and write functionalities. Let’s look at the methods of Immutable collections :

List: ArrayList<T>(), arrayListOf(), mutableListOf()

Map: hashMapOf(), mutableMapOf()

Set: hashSetOf(), mutableSetOf()

Kotlin – List

Its an interface and generic collection of elements. The List interface inherits from Collection<T> class. It is immutable and its methods support only read functionalities.

The elements of List follow the sequence of the insertion order and contain index number same as an array.

List Interface Declaration

public interface List<out E> : Collection<E> (source)

functions

Simple example:

fun main(args: Array<String>){  
    var list = listOf("Ram","Shyam","Mahendra")//This is read only and fixed-size  
    for(element in list){  
        println(element)  
    }  

//Output:
Ram
Shyam
Mahendra

Another simple example:

fun main(args: Array<String>){  
    var stringList: List<String> = listOf<String>("Amar","Akbar","Anthony","Rahul","Ritu")  
    var list: List<String> = listOf<String>("Amar","Akbar","Anthony")  
    for(element in stringList){  
        print(element+" ")  
    }  
    println()  
    println(stringList.get(0))  
    println(stringList.indexOf("Rahul"))  
    println(stringList.lastIndexOf("Rahul"))  
    println(stringList.size)  
    println(stringList.contains("Anthony"))  
    println(stringList.containsAll(list))  
    println(stringList.subList(2,4))  
    println(stringList.isEmpty())  
    println(stringList.drop(1))  
    println(stringList.dropLast(2))  
}  

//Output:
Amar Akbar Anthony Rahul Ritu 
Amar
3
3
5
true
true
[Anthony, Rahul]
false
[Akbar, Anthony, Rahul, Ritu]
[Amar, Akbar, Anthony]

Kotlin – MutableList(mutableListOf())

It can be called as an interface and generic collection of elements, which is mutable in nature. It inherits from Collection<T> class. The methods of this interface support both read and write functionalities. After declaration of elements in MutableList, more elements can be added/removed from it (no fixed size length).

For using MutableList interface, functions can be used –  mutableListOf() or mutableListOf<E>().

The elements of MutableList follow a sequence of the insertion order and contains index number same as array.

MutableList Interface Declaration

interface MutableList<E> : List<E>, MutableCollection<E> (source)

Simple example:

fun main(args: Array<String>){  
    var mutableList = mutableListOf("Suresh","Mahesh","Rajesh","Brijesh")  
  
    for(element in mutableList){  
        println(element)  
    }  
    println()  
    for(index in 0..mutableList.size-1){  
        println(mutableList[index])  
    }  
}  

//Output:
Suresh
Mahesh
Rajesh
Brijesh

Suresh
Mahesh
Rajesh
Brijesh

Another example:

fun main(args: Array<String>){  
    var mutableList1 = mutableListOf("Ram","Suresh")  
    mutableList1.add("Paramjeet")  
    mutableList1.add("Ramnaresh")  
  
    var mutableList2 = mutableListOf<String>()  
    mutableList2.add("Rajiv")  
    mutableList2.add("Ramlal")  
    mutableList2.add("Singham")  
  
    for(element in mutableList1){  
        println(element)  
    }  
    println()  
    for(element in mutableList2){  
        println(element)  
    }  
}  

//Output:
Ram
Suresh
Paramjeet
Ramnaresh

Rajiv
Ramlal
Singham

Kotlin ArrayList class

This class is used to create a dynamic array, meaning its size can be increased or decreased according to requirement. This class provides both read and write functionalities.

While following the sequence of insertion order. ArrayList class is non synchronized and it may contains duplicate elements. The elements of ArrayList class are accessed randomly as it works on index basis.

Constructor of Kotlin ArrayList

Functions of Kotlin ArrayList

Simple example:

fun main(args: Array<String>){  
  
    val arrayList = ArrayList<String>()//Creating an empty arraylist  
    arrayList.add("Samuel")//Adding object in arraylist  
    arrayList.add("Diablo")  
    arrayList.add("John")  
    arrayList.add("Rakesh")  
    arrayList.add("Jinpin")  
    println(".......printing ArrayList.......")  
    for (i in arrayList) {  
        println(i)  
    }  
}  

//Output:
.......printing ArrayList.......
Samuel
Diablo
John
Rakesh
Jinpin

Kotlin ArrayList: arrayListOf

arrayListOf() is one of function of ArrayList class. ArrayList is mutable, providing both read and write functionalities. This function returns an ArrayList type.

Syntax of arrayListOf() function:

inline fun <T> arrayListOf(): ArrayList<T> (source)  
fun <T> arrayListOf(vararg elements: T): ArrayList<T> (source) 

Syntax of arrayListOf() function: