EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Kotlin Collections

Computer Science / Kotlin tutorial chapter - Published 2025-12-17 - Kotlin

In Kotlin, collections are a key feature used to store and manage groups of data. Kotlin offers several types of collections, such as ListsSetsMapsArrays, and Sequences, each with distinct characteristics for handling data efficiently.

Types of Kotlin Collections

1. List: An ordered collection that allows duplicate elements. Elements can be accessed by their index.
2. Set: An unordered collection that holds unique elements, ensuring no duplicates.
3. Map: A collection of key-value pairs where each key is unique, but values can be duplicated.
4. Array: A fixed-size collection of elements of the same type.
5. Sequence: A lazily evaluated collection that can be processed step-by-step.

Example of Using a List in Kotlin:

val cities = listOf("New York", "London", "Paris", "Tokyo")

// Access elements
println("First city: ${cities[0]}")
println("Last city: ${cities.last()}")

// Iterate over the list
for (city in cities) {
    println(city)
}

// Filter the list
val filteredCities = cities.filter { it.startsWith("L") }
println("Filtered cities: $filteredCities")

Output:

First city: New York
Last city: Tokyo
New York
London
Paris
Tokyo
Filtered cities: [London]

Explanation: In this example, we create an immutable list of fruits, access elements, iterate over the list, and filter elements that start with the letter “a”.

End of lesson.