What are the collections in Kotlin Language? | Kotlin - Collections in kotlin language
What are the collections in Kotlin Language?
In this article, you will learn about the following pointers:
- What are collections?
- How many types of collections does kotlin have?
- How to create the list, sets, maps in kotlin?
Most programming languages use the concept of collections. In layman language, a collection is a group of objects of a similar or different kind. Similarly, in programming languages, the collection is a group of objects of the same kind. The object is an item or element in the collection.
The kotlin has different collections. The kotlin also has a set of tools to manage the collection. The type of collections are:
- List: It is a collection of non-unique elements that implies one element can occur twice in the list. It is an ordered collection that is accessed by the element indices.
- Set: It is a collection of unique elements. It means the repetition of elements is not possible.
- Map: Mostly, it is also known as a dictionary. It is a set of key-value pairs. The key must be unique. Each key is mapped with one value.
It is mainly divided into two categories:
- Kotlin Mutable Collections
- Kotlin Immutable Collections
Kotlin Immutable Collections
Immutable collections provide the read-only methods. In simple words, it means once the collection is created it cannot be changed or altered by any means.
Methods that fall under this category are:
- List — listOf() and listOf<T>()
- Map — mapOf()
- Set — setOf()
Kotlin Mutable collections
Mutable collections provide both read and write methods. In this, we can change the collection once it is created.
Methods that fall under this category are:
- List — ArrayList<T>(), arrayListOf(), and mutableListOf()
- Map — HashMap, hashMapOf(), and mutableMapOf()
- Set — hashSetOf() and mutableSetOf()
How to create the list in kotlin?
listOf() is the standard library function that is used to create an immutable list and mutableListOf() is used to create the mutable list in kotlin.
Example:
fun main() {
val kotlin_theList = listOf("apple", "mango", "orange", "pineapple")
println(kotlin_theList)
val kotlin_theMutableList = mutableListOf("apple", "mango", "orange", "pineapple")
println(kotlin_theMutableList)
}
The output of the following code is:
[apple, mango, orange, pineapple]
[apple, mango, orange, pineapple]
How to create the sets in kotlin?
There are library functions that are used in creating sets such as setOf() to create the immutable list and mutableSetOf() to create the mutable sets.
Example:
fun main() {
val kotlin_Set = setOf("ciaz", "wa", "three", "four")
println(kotlin_theSet)
val kotlin_theMutableSet = mutableSetOf("one", "two", "three", "four")
println(kotlin_theMutableSet)
}
The output of the following code:
[one, two, three, four]
[one, two, three, four]
How to create the Maps in Kotlin?
mapOf() is used to create the immutable maps and mutableMapOf() is used to create the mutable maps.
Example:
fun main() {
val kotlin_theMap = mapOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(kotlin_theMap)
val kotlin_theMutableMap = mutableSetOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(kotlin_theMutableMap)
}
When a user runs the code, it will give the following output:
{one=1, two=2, three=3, four=4}
[(one, 1), (two, 2), (three, 3), (four, 4)]