Open In App

Destructuring Declarations in Kotlin

Last Updated : 14 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Kotlin provides a concise and elegant way to extract multiple values from an object into separate variables using destructuring declarations. This feature allows you to break an object into its components and work with each piece independently.

Destructuring Declaration

A destructuring declaration unpacks an object into multiple variables at once. It is especially useful when dealing with classes that hold multiple related values (like data classes, pairs, or maps).

For example :

val (empId, salary) = employee
println("$empId $salary")

Here, empId and salary are automatically extracted from the employee object.

Component Functions

Destructuring declarations rely on a set of special functions named component1(), component2(), ..., componentN(). These are called component functions, and each corresponds to a variable in the destructuring declaration.

When we write

val (a, b) = someObject

The compiler translates it to:

val a = someObject.component1()
val b = someObject.component2()

To support destructuring, a class must implement the required componentN() functions. These must be marked with the operator modifier.


Data Classes and Destructuring

Kotlin’s data classes automatically generate componentN() functions for all the properties declared in the primary constructor. This makes them ideal for destructuring.

Example:

Kotlin
data class Employee(val id: Int, val salary: Double)

fun main() {
    val employee = Employee(101, 50000.0)
    val (empId, empSalary) = employee
    println("ID: $empId, Salary: $empSalary")
}


Output:

ID: 101, Salary: 50000.0


Returning Multiple Values from a Function

You can use destructuring to conveniently return and unpack multiple values from a function using Pair, Triple, or a custom data class.

Example:

Kotlin
data class Person(val name: String, val age: Int)

fun getPersonDetails(): Person {
    return Person("Jack", 30)
}

fun main() {
    val (name, age) = getPersonDetails()
    println("Name is $name Age is $age")
}


Output:

Name is Jack Age is 30


Ignoring Unused Variables with Underscore (_)

Sometimes, we may not need all the values. We can skip them using an underscore _. This avoids unnecessary calls to componentN() functions. For example:

val (_, salary) = employee
println("Only salary: $salary")

Here, the component1() (for ID) is not called.


Destructuring declaration in lambdas

Since Kotlin 1.1, the destructing declaration syntax can be used for lambda parameters also. If a lambda parameter has a parameter of Pair type or some other type that declares component functions, then we can introduce new parameters by putting them in parenthesis. The rules are the same as defined above.

Example:

Kotlin
fun main() {
    val map = mapOf(
        1 to "Ishita", 2 to "Kamal", 3 to "Kanika",
        4 to "Minal", 5 to "Neha", 6 to "Pratyush",
        7 to "Shagun", 8 to "Shashank", 9 to "Uday", 10 to "Vandit"
    )

    println("Initial map is $map")

    // Destructuring a map entry into key and values
    val updatedMap = map.mapValues { (_, value) -> "Hello $value" }

    println("Map after appending Hello $updatedMap")
}


Output:

Initial map is {1=Ishita, 2=Kamal, 3=Kanika, 4=Minal, 5=Neha, 6=Pratyush, 7=Shagun, 8=Shashank, 9=Uday, 10=Vandit}
Map after appending Hello {1=Hello Ishita, 2=Hello Kamal, 3=Hello Kanika, 4=Hello Minal, 5=Hello Neha, 6=Hello Pratyush, 7=Hello Shagun, 8=Hello Shashank, 9=Hello Uday, 10=Hello Vandit}

If a component of the destructured parameter is not in use, we can replace it with the underscore to avoid calling component function:

map.mapValues { (_,value) -> "Hello $value" }


Custom Component Functions

We can also define our own componentN() functions in non-data classes, but we must mark them with the operator keyword.

Example:

Kotlin
class Coordinates(val x: Int, val y: Int) {
    operator fun component1() = x
    operator fun component2() = y
}

fun main() {
    val point = Coordinates(5, 10)
    val (x, y) = point
    println("x: $x, y: $y")
}

Output:

x: 5, y: 10



Article Tags :

Similar Reads