Language Elements

Data Types

Kotlin Nullable Types

Extension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Collections

Kotlin Example Codes

Kotlin Interview Questions

Kotlin's Standard Library offers generic interfaces, classes, and functions for creating, populating, and managing collections efficiently and concisely. The supported collections are based on three basic data structures: List, Set, and Map.

  • List: An ordered collection of elements that can be accessed by indices. Elements can occur more than once in a list.

  • Set: A collection of unique elements where the order does not matter. Duplicate elements are not allowed.

  • Map (or dictionary): A collection of key-value pairs. Keys are unique, and each key maps to exactly one value. Values, however, can have duplicates.
  • Kotlin collections are categorized as mutable or immutable, based on whether their contents can be modified after creation.

    Mutable Collections

    Mutable collections support both read and write operations, allowing elements to be added, removed, or updated. Examples include mutable List, Set, and Map.
    val mutableList = mutableListOf("apple", "banana", "cherry")
    
    mutableList.add("date")
    mutableList.removeAt(1)
    mutableList[0] = "apricot"
    
    val mutableSet = mutableSetOf("apple", "banana", "cherry")
    
    mutableSet.add("date")
    mutableSet.remove("banana")
    
    val mutableMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
    
    mutableMap["four"] = 4
    mutableMap.remove("two")
    

    Immutable Collections

    Immutable collections support only read operations, meaning their elements cannot be modified once the collection is created. Examples include immutable List, Set, and Map.
    val immutableList = listOf("apple", "banana", "cherry")
    
    val immutableSet = setOf("apple", "banana", "cherry")
    
    val immutableMap = mapOf("one" to 1, "two" to 2, "three" to 3)
    



    Copyright © by Zafar Yasin. All rights reserved.