Destructuring declarations in Kotlin simplify the process of unpacking values from objects, data classes, maps, arrays, or collections into individual variables. This is particularly useful when working with complex objects or collections. Below is an explanation of how to use destructuring declarations in different scenarios.
1. Data Classes
Destructuring declarations work seamlessly with data classes. By default, each data class generates componentN() functions for its properties, allowing destructuring.
data class Person(val name: String, val age: Int)
fun main() {
val person = Person("Alice", 25)
// Destructure into two variables
val (name, age) = person
println("Name: $name, Age: $age") // Output: Name: Alice, Age: 25
}
2. Pairs and Triples
Kotlin’s Pair and Triple classes also support destructuring.
fun main() {
val pair = Pair("Kotlin", 2023)
val (language, year) = pair
println("Language: $language, Year: $year") // Output: Language: Kotlin, Year: 2023
val triple = Triple("A", "B", "C")
val (first, second, third) = triple
println("$first, $second, $third") // Output: A, B, C
}
3. Maps
For maps, destructuring can be used in for loops.
fun main() {
val map = mapOf("A" to 1, "B" to 2)
for ((key, value) in map) {
println("$key -> $value")
}
}
4. Destructuring in Functions
You can destructure function parameters if they accept a Pair, Triple, or a destructurable object.
fun printPair(pair: Pair<String, Int>) {
val (key, value) = pair
println("$key -> $value")
}
fun main() {
val pair = "Kotlin" to 2023
printPair(pair) // Output: Kotlin -> 2023
}
Or alternatively:
fun printPairDestructured(key: String, value: Int) {
println("$key -> $value")
}
fun main() {
val pair = "Kotlin" to 2023
printPairDestructured(pair.first, pair.second)
// OR use `destructuring` combined with named parameters
val (key, value) = pair
println("Declarative alternative using external scoped vars $key $value")
}
5. Arrays and Lists
You can destructure arrays and lists up to the number of elements you want to extract.
fun main() {
val numbers = listOf(1, 2, 3)
val (first, second, third) = numbers
println("$first, $second, $third") // Output: 1, 2, 3
val array = arrayOf(10, 20, 30)
val (a, b, c) = array
println("$a, $b, $c") // Output: 10, 20, 30
}
6. Custom Destructuring
If you want a custom class to support destructuring, you need to implement componentN() functions for the properties you want to destructure.
class Coordinates(val x: Int, val y: Int) {
operator fun component1() = x
operator fun component2() = y
}
fun main() {
val point = Coordinates(10, 20)
val (x, y) = point
println("x: $x, y: $y") // Output: x: 10, y: 20
}
7. Underscore (_) for Ignoring Values
If you don’t need all the components, you can ignore specific values using _.
fun main() {
val person = Person("Alice", 25)
val (name, _) = person
println("Name: $name") // Output: Name: Alice
}
8. With Loops
Destructuring is helpful in loops to extract values efficiently.
fun main() {
val list = listOf(Pair("A", 1), Pair("B", 2))
for ((char, number) in list) {
println("$char -> $number")
}
}
Notes:
- Destructuring works because Kotlin generates
componentN()functions automatically fordata classproperties. - The number of variables in the destructuring declaration must match the number of
componentN()functions available for the object.
By using destructuring, you can write code that is more concise and easier to read.
