![]() |
VOOZH | about |
In Kotlin, collections are used to store and manipulate groups of objects. Kotlin provides a rich collection framework through its standard library, offering both read-only (immutable) and modifiable (mutable) collections.
Collection Name | Description |
|---|---|
Lists | Ordered collections of elements that allow duplicates. |
Sets | Unordered collections of unique elements. |
Maps | Collections of key-value pairs, where each key is unique. |
Arrays | Fixed-size collections of elements with a specific type. |
Sequences | Lazily evaluated collections of elements that can be processed in a pipeline. |
Example:
Output
First fruit: apple
Last fruit: grape
apple
banana
orange
grape
Filtered list: [apple]
Explanation:
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.
In Kotlin, collections are categorized into two forms.
Immutable collections in Kotlin are read-only collections, meaning their elements cannot be added, removed, or modified after creation. They help ensure data safety and predictability, especially when shared across different parts of a program. Kotlin provides three main immutable collection types:
An immutable list is an ordered collection that allows duplicate elements. Elements can be accessed using their index, but the list cannot be modified once created. It is created using the listOf() function and supports only read operations.
Example
Output:
Ram
Ram
Raj
Sita
An immutable set is an unordered collection that stores unique elements only. Duplicate values are automatically removed, and the collection does not support modification. It is created using the setOf() function.
Example
Output: (Order may vary)
6
9
0
Immutable map stores data in key–value pairs, where each key is unique and mapped to exactly one value. The map size and entries cannot be modified after creation. It is created using the mapOf() function and supports read-only access using keys.
Example
Output:
Ram
Raj
Sita
Mutable collections in Kotlin support both read and write operations. Elements can be added, updated, or removed after the collection is created. These collections are useful when the data needs to change during program execution. Kotlin provides three main mutable collection types:
A mutable list is an ordered collection that allows duplicate elements and supports modification operations such as add, remove, and element updates. It is commonly created using the mutableListOf() function.
Example
Output:
Laxman
Ram
Sita
Ravan
A mutable set stores unique elements only and supports modification operations like add and remove. Duplicate values are ignored, and element order is not guaranteed. It is created using mutableSetOf().
Example:
Output: (Order may vary)
6
10
2
5
A mutable map stores data as key–value pairs and allows updating existing values or adding new entries. Keys must be unique, while values can be duplicated. It is commonly created using mutableMapOf().
Example
Output:
Laxman
Ram
Sita
Ravan