Collections are fundamental data structures used in programming to store and manage groups of objects. In Kotlin, collections provide a rich set of functionalities that simplify tasks such as iteration, filtering, mapping, and transforming data. This tutorial will cover the basics of collections in Kotlin, including lists, sets, maps, and their mutable counterparts.
Collections in Kotlin are categorized into two main types: mutable and immutable. Mutable collections can be modified after they are created, while immutable collections cannot be changed once they are initialized.
Lists are the most commonly used collections in Kotlin. They maintain the order of elements and can contain duplicates.
Kotlin provides several ways to create lists:
// Immutable list
val immutableList = listOf("Apple", "Banana", "Cherry")
// Mutable list
val mutableList = mutableListOf("Apple", "Banana", "Cherry")
Elements in a list can be accessed using indices:
println(immutableList[0]) // Output: Apple
println(mutableList[1]) // Output: Banana
Mutable lists allow adding and removing elements:
mutableList.add("Date")
mutableList.removeAt(2)
Sets are collections that do not allow duplicate elements. They are useful when you need to ensure uniqueness.
Kotlin provides two types of sets: Set and MutableSet.
// Immutable set
val immutableSet = setOf("Apple", "Banana", "Cherry")
// Mutable set
val mutableSet = mutableSetOf("Apple", "Banana", "Cherry")
Mutable sets allow adding and removing elements:
mutableSet.add("Date")
mutableSet.remove("Banana")
Maps store key-value pairs, where each key is unique. They are useful for associating data.
Kotlin provides two types of maps: Map and MutableMap.
// Immutable map
val immutableMap = mapOf("Apple" to 10, "Banana" to 20, "Cherry" to 30)
// Mutable map
val mutableMap = mutableMapOf("Apple" to 10, "Banana" to 20, "Cherry" to 30)
Values in a map can be accessed using keys:
println(immutableMap["Apple"]) // Output: 10
println(mutableMap["Banana"]) // Output: 20
Mutable maps allow adding and removing entries:
mutableMap.put("Date", 40)
mutableMap.remove("Cherry")
Kotlin provides a rich set of functions for common operations on collections.
Iterating over a collection is straightforward:
for (item in immutableList) {
println(item)
}
Filtering elements based on a condition:
val filteredList = immutableList.filter { it.startsWith("B") }
println(filteredList) // Output: [Banana]
Transforming elements of a collection:
val upperCaseList = immutableList.map { it.toUpperCase() }
println(upperCaseList) // Output: [APPLE, BANANA, CHERRY]
Reducing a collection to a single value:
val sumOfValues = mutableMap.values.sum()
println(sumOfValues) // Output: 60
List when order matters and duplicates are allowed. Use Set when uniqueness is required. Use Map for key-value associations.filter, map, and reduce to write concise and readable code.Collections in Kotlin provide powerful tools for managing groups of objects. By understanding the differences between mutable and immutable collections, and by leveraging Kotlin's rich set of collection operations, you can write efficient and expressive code. Whether you're working on a small script or a large-scale application, mastering collections will greatly enhance your productivity.
This tutorial provides a comprehensive overview of collections in Kotlin, equipping you with the knowledge to effectively manage and manipulate data in your applications.