Introduction
Collections in Kotlin are used to store groups of related objects. Kotlin provides a variety of collection types, such as lists, sets, and maps, that can hold multiple items. These collections come in two variants: mutable and immutable. Immutable collections cannot be modified after they are created, while mutable collections can be. This chapter will cover the basics of Kotlin collections, including lists, sets, maps, and operations you can perform on them.
Lists
A list is an ordered collection of elements that can contain duplicates. Kotlin provides List
and MutableList
interfaces to handle lists.
Immutable List
An immutable list cannot be modified after it is created.
Syntax
val list: List<Type> = listOf(elements)
Example
fun main() {
val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
println(fruits)
}
Explanation:
val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
: Creates an immutable list of strings.
Output:
[Apple, Banana, Cherry]
Mutable List
A mutable list can be modified after it is created.
Syntax
val list: MutableList<Type> = mutableListOf(elements)
Example
fun main() {
val fruits: MutableList<String> = mutableListOf("Apple", "Banana")
fruits.add("Cherry")
println(fruits)
}
Explanation:
val fruits: MutableList<String> = mutableListOf("Apple", "Banana")
: Creates a mutable list of strings.fruits.add("Cherry")
: Adds an element to the mutable list.
Output:
[Apple, Banana, Cherry]
Sets
A set is a collection of unique elements. Kotlin provides Set
and MutableSet
interfaces to handle sets.
Immutable Set
An immutable set cannot be modified after it is created.
Syntax
val set: Set<Type> = setOf(elements)
Example
fun main() {
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
println(fruits)
}
Explanation:
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
: Creates an immutable set of strings.
Output:
[Apple, Banana, Cherry]
Mutable Set
A mutable set can be modified after it is created.
Syntax
val set: MutableSet<Type> = mutableSetOf(elements)
Example
fun main() {
val fruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
fruits.add("Cherry")
println(fruits)
}
Explanation:
val fruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
: Creates a mutable set of strings.fruits.add("Cherry")
: Adds an element to the mutable set.
Output:
[Apple, Banana, Cherry]
Maps
A map is a collection of key-value pairs. Kotlin provides Map
and MutableMap
interfaces to handle maps.
Immutable Map
An immutable map cannot be modified after it is created.
Syntax
val map: Map<KeyType, ValueType> = mapOf(key to value, ...)
Example
fun main() {
val fruits: Map<String, Int> = mapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)
println(fruits)
}
Explanation:
val fruits: Map<String, Int> = mapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)
: Creates an immutable map of strings to integers.
Output:
{Apple=1, Banana=2, Cherry=3}
Mutable Map
A mutable map can be modified after it is created.
Syntax
val map: MutableMap<KeyType, ValueType> = mutableMapOf(key to value, ...)
Example
fun main() {
val fruits: MutableMap<String, Int> = mutableMapOf("Apple" to 1, "Banana" to 2)
fruits["Cherry"] = 3
println(fruits)
}
Explanation:
val fruits: MutableMap<String, Int> = mutableMapOf("Apple" to 1, "Banana" to 2)
: Creates a mutable map of strings to integers.fruits["Cherry"] = 3
: Adds a key-value pair to the mutable map.
Output:
{Apple=1, Banana=2, Cherry=3}
Collection Operations
Kotlin provides various operations that can be performed on collections, such as filtering, mapping, and more.
Filtering
Filtering returns a list containing only elements that match a given predicate.
Example
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers)
}
Explanation:
numbers.filter { it % 2 == 0 }
: Filters the list to include only even numbers.
Output:
[2, 4]
Mapping
Mapping transforms each element in a collection based on a given function.
Example
fun main() {
val numbers = listOf(1, 2, 3)
val squares = numbers.map { it * it }
println(squares)
}
Explanation:
numbers.map { it * it }
: Maps each element to its square.
Output:
[1, 4, 9]
Example Program with Collections
Here is an example program that demonstrates various aspects of using collections in Kotlin:
fun main() {
// Immutable list
val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
println(fruits)
// Mutable list
val mutableFruits: MutableList<String> = mutableListOf("Apple", "Banana")
mutableFruits.add("Cherry")
println(mutableFruits)
// Immutable set
val fruitSet: Set<String> = setOf("Apple", "Banana", "Cherry")
println(fruitSet)
// Mutable set
val mutableFruitSet: MutableSet<String> = mutableSetOf("Apple", "Banana")
mutableFruitSet.add("Cherry")
println(mutableFruitSet)
// Immutable map
val fruitMap: Map<String, Int> = mapOf("Apple" to 1, "Banana" to 2, "Cherry" to 3)
println(fruitMap)
// Mutable map
val mutableFruitMap: MutableMap<String, Int> = mutableMapOf("Apple" to 1, "Banana" to 2)
mutableFruitMap["Cherry"] = 3
println(mutableFruitMap)
// Filtering
val numbers = listOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers)
// Mapping
val squares = numbers.map { it * it }
println(squares)
}
Output:
[Apple, Banana, Cherry]
[Apple, Banana, Cherry]
[Apple, Banana, Cherry]
[Apple, Banana, Cherry]
{Apple=1, Banana=2, Cherry=3}
{Apple=1, Banana=2, Cherry=3}
[2, 4]
[1, 4, 9, 16, 25]
Conclusion
In this chapter, you learned about collections in Kotlin, including how to work with lists, sets, and maps. You also learned about immutable and mutable collections and various collection operations like filtering and mapping. Understanding and using collections effectively is crucial for writing robust and maintainable Kotlin programs.