In this article , lets discuss about Kotlin Collections
Collections are used to store and manipulate group of objects or data
Ordered Collection is the collection in which the position of each item is fixed Eg: Lists , Array , etc
Unordered Collection is the collection in which the position of each item is not fixed which means order of all elements are not maintained. Eg: Set
Collection Types
Immutable Collection
This collection supports read only functionality and we cannot modify its elements
List
This is an ordered collection in which elements can be accessed using indices
Elements can be repeated in list for any number of times
We cannot perform add/remove operations in this immutable list
listOf() , listOf<T>()
val androidOsName = listOf(
"Upside Down Cake",
"Vanilla Ice Cream",
"Tiramisu",
"Snow Cone",
"Red Velvet Cake"
)
for (name in androidOsName){
println(name)
}
Set
This is an unordered collection in which elements cannot be duplicated
Set is collection of unique elements
We cannot perform add/remove operations because of immutable collection setOf()
val android14Info = setOf(
"Android 14" ,
"Upside Down Cake" ,
14 ,
34 ,
"October 4 2023 "
)
for (name in android14Info){
println(name)
}
Map
Maps are unordered collection of set of key/value pairs
Map key are unique and can hold only one value for each key
Keys are unique but values are duplicated
Map is used to store logical collection between two objects
As it is immutable , size is fixed and supports read only access .
mapOf()
val androidOsInfo = mapOf(
13 to "Android 13 - Tiramisu - 13 - 33 - August 15, 2022 " ,
12 to "Android 12 - Snow Cone - 12 - 31 - October 4, 2021 " ,
11 to "Android 11 - Red Velvet Cake[24] - 11 - 30 tSeptember 8, 2020 "
)
for (info in androidOsInfo) {
println("${info.key} -> ${info.value}")
}
Mutable Collection
This collection supports both read and write functionality.
List
This mutable list supports both read and write operation
The declared elements in list can be either removed or added
mutableListOf() , arrayListOf<T>() , ArrayList
val androidOsName = mutableListOf(
"Upside Down Cake",
"Vanilla Ice Cream",
"Tiramisu"
)
// modify item
androidOsName[0] = "Pistachio Ice Cream"
// add item
androidOsName.add("Snow Cone")
for (name in androidOsName){
println(name)
}
Set
This set supports both read and write
Set allows us to easily to add/remove elements
Order of elements are preserved
mutableSetOf() , hashSetOf()
val androidOsName = mutableSetOf<String>(
"Upside Down Cake",
"Vanilla Ice Cream",
"Tiramisu"
)
// add item
androidOsName.add("Snow Cone")
// remove item
androidOsName.remove("Upside Down Cake")
for (name in androidOsName){
println(name)
}
Map
Since this Map is mutable and this supports operations like put, remove , clear ,etc
mutableMapOf() , hashMapOf() and HashMap
val androidOsInfo = mutableMapOf(
13 to "Android 13 - Tiramisu - 13 - 33 - August 15, 2022 ",
12 to "Android 12 - Snow Cone - 12 - 31 - October 4, 2021 ",
11 to "Android 11 - Red Velvet Cake[24] - 11 - 30 tSeptember 8, 2020 "
)
// add item
androidOsInfo.put(10, "Android 10 - Quince Tart[24] - 10 - 29 - September 3, 2019 ")
// remove item
androidOsInfo.remove(12)
for (info in androidOsInfo) {
println("${info.key} -> ${info.value}")
}
So finally, I hope I made somebody understand the basics of kotlin collections
Please leave your comments to improve.
Happy and Enjoy coding