Kotlin Collections

Kotlin also introduced the concept of collections. A collection sometimes contains a number of objects of constant type and these objects within the collection ar referred to as components or things. Kotlin customary Library provides an expensive set of tools for managing collections.

Types of Collections

In Kotlin collections are two categories into forms.

  1. Immutable Collection
  2. Mutable Collection

1. Immutable Collection

It means it supports only read-only functionalities and may not be changed its components. immutable Collections and their corresponding strategies are:

List – listOf() and listOf<T>()
Set – setOf()
Map – mapOf()

List

It is an ordered collection during which we will access components or things by victimization indices – number numbers that outline a grip for every component. components may be perennial during a list any variety of times. we will not perform add or take away operations within the immutable list. Kotlin program to demonstrate this list:

fun main(args: Array<String>) {
    val immutableList = listOf("mani","kani","sani")
  
    // gives compile time error
    // immutableList.add = "loni"
  
    for(item in immutableList){
        println(item)
    }
}

Output:

mani
kani
sani

Set

It is a collection of unordered components additionally it doesn’t support duplicate components. it’s a group of distinctive components. Generally, the order of set components doesn’t have a major impact. we will not perform add or take away operations as a result of it’s an immutable Set. Kotlin program to demonstrate this set:

fun main(args: Array<String>) {

    // initialize with duplicate values
    // but output with no repetition
    
    var immutableSet = setOf(6,9,9,0,0,"mahi","nahi")
  
    // gives compile time error
    // immutableSet.add(7)
  
    for(item in immutableSet){
        println(item)
    }
}

Output:

6
9
0
mahi
nahi

Map

Map keys are unique and hold only 1 worth for every key, it’s a group of key-value pairs. every key maps to precisely one worth. The values may be duplicates however keys ought to be distinctive. Maps are wont to store logical connections between 2 objects, for instance, a student ID and their name. because it is immutable its size is mounted and its methods support read-only access. Kotlin program to demonstrate this map:

fun main(args : Array<String>) {
    var immutableMap = mapOf(9 to "mahi",8 to "nahi",7 to "rahi")
  
    // gives compile time error
    // immutableMap.put(9,"Praveen")
  
    for(key in immutableMap.keys){
        println(immutableMap[key])
    }
}

Output:

mahi
nahi
rahi

2. Mutable Collection

It supports both scan and write functionalities. mutable collections and their corresponding strategies are:

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

List

Since mutable list supports scan and write operation, declared components within the list will either be removed or another. Kotlin program to demonstrate this list:

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

Output:

pahi
nahi
rahi
Abhi

Set

The mutable Set supports each scan and write practicality. we will access add or take away components from the collections simply and it’ll preserve the order of the weather. Kotlin program to demonstrate this set:

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)
    }
}

Output:

6
10
2
5

Map

It is mutable thus it supports functionalities like place, remove, clear, etc. Kotlin program to demonstrate this map.

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

Output:

pahi
nahi
rahi
Abhi

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories