Kotlin Collections

Similar to Java Collections, Kotlin also introduced the concept of collections. A collection usually contains a number of objects of the same type and these objects in the collection are called elements or items.

Kotlin Standard Library provides a rich set of tools for managing collections.

Types of Collections –
In Kotlin collections are categories into two forms.



  1. Immutable Collection
  2. Mutable Collection

Immutable means that it supports only read-only functionalities and can not be modified its elements. Immutable Collections and their corresponding methods are:

  1. List – listOf() and listOf<T>()
  2. Set – setOf()
  3. Map – mapOf()

List – It is an ordered collection in which we can access to elements or items by using indices – integer numbers that define position for each element. Elements can be repeated in a list any number of times. We can perform add or remove operations in the immutable list.

Kotlin program to demonstrate the immutable list –

filter_none

edit
close

play_arrow

link
brightness_4
code

// An example for immutable list
fun main(args: Array<String>) {
    val immutableList = listOf("Mahipal","Nikhil","Rahul")
    // gives compile time error 
    // immutableList.add = "Praveen"
    for(item in immutableList){
        println(item)
    }
}

chevron_right


Output:

Mahipal
Nikhil
Rahul

 

Set – It is a collection of unordered elements also it does not support duplicate elements. It is a collection of unique elements. Generally, the order of set elements does not has significant effect. We can not perform add or remove operations because it is immutable Set.

Kotlin program to demonstrate the immutable set –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    // initialize with duplicate values but output with no repeatition
    var immutableSet = setOf(6,9,9,0,0,"Mahipal","Nikhil")
    // gives compile time error
    //immutableSet.add(7)
    for(item in immutableSet){
        println(item)
    }
}

chevron_right


Output:


6
9
0
Mahipal
Nikhil

 

Map – Map keys are unique and holds only one value for each key, it is a set of key-value pairs. Each key maps to exactly one value. The values can be duplicates but keys should be unique. Maps are used to store logical connections between two objects, for example, a student ID and their name. As it is immutable it’s size is fixed and it’s methods supports read-only access.

Kotlin program to demonstrate the immutable map –

filter_none

edit
close

play_arrow

link
brightness_4
code

// An example for immutable map
fun main(args : Array<String>) {
    var immutableMap = mapOf(9 to "Mahipal",8 to "Nikhil",7 to "Rahul")
    // gives compile time error
    // immutableMap.put(9,"Praveen")
    for(key in immutableMap.keys){
        println(immutableMap[key])
    }
}

chevron_right


Output:

Mahipal
Nikhil
Rahul

 

Mutable Collection supports both read and write functionalities. Mutable collections and their corresponding methods are:

  1. List – mutableListOf(),arrayListOf() and ArrayList
  2. Set – mutableSetOf(), hashSetOf()
  3. Map – mutableMapOf(), hashMapOf() and HashMap

List – Since mutable list supports read and write operation, declared elements in the list can either be removed or added.

Kotlin program to demonstrate the mutable list –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var mutableList = mutableListOf("Mahipal","Nikhil","Rahul")
    // we can modify the element
    mutableList[0] = "Praveen"
    // add one more element in the list
    mutableList.add("Abhi")
    for(item in mutableList){
        println(item)
    }
}

chevron_right


Output:

Mahipal
Nikhil
Rahul
Abhi

 


Set – The mutable Set supports both read and write functionality. We can access add or remove elements from the collections easily and it will preserve the order of the elements.

Kotlin program to demonstrate the mutable list –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var mutableSet = mutableSetOf<Int>(6,10)
    // adding elements in set
    mutableSet.add(2)
    mutableSet.add(5)
    for(item in mutableSet){
        println(item)
    }
}

chevron_right


Output:

6
10
2
5

 

Map – It is mutable so it supports functionalities like put, remove, clear etc.

Kotlin program to demonstrate the mutable list –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var mutableMap = mutableMapOf<Int,String>(1 to "Mahipal",2 to "Nikhil",3 to "Rahul")
    // we can modify the element
    mutableMap.put(1,"Praveen")
    // add one more element in the list
    mutableMap.put(4,"Abhi")
    for(item in mutableMap.values){
        println(item)
    }
}

chevron_right


Output:

Praveen
Nikhil
Rahul
Abhi


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.