Updated February 14, 2023
Introduction to Kotlin Destructuring
Kotlin destructuring in kotlin is used to initialize and create multiple variables simultaneously. Kotlin provides the programmer a unique way of working with instances class with a declaration, which was destructuring. Various variables correspond to the class of properties associated with an example. We can use the variable independently in any way. It is beneficial and essential for creating multiple variables.
Key Takeaways
- In kotlin, multiple ways to extract the data from the collection are used by an index and destructuring declarations. Kotlin gives us the tool for efficiently extracting the data from a collection.
- In kotlin, we are making destructuring declarations only when our object will require component functions.
What is Kotlin Destructuring?
Kotlin will contain many features in the language of programming. It will allow us to declare multiple variables simultaneously; this technique is called the destructuring of kotlin. Suppose we want to break down the object which contains the various fields which were initializing variable which was separate for allowing the same we are using destructive declarations into the kotlin declaration; destructuring variables will enable us to define the values and variables at the local level. Using it is a convenient way to define local variables and values; we can determine any local value.
How to Use Destructuring Declarations?
In kotlin, sometimes it is very convenient to destructure the object into many variables by using the destructuring declarations.
Below syntax shows how we can declare the destructuring in kotlin as follows:
Syntax:
val (variable1, variable2) = object_name
The above syntax is called the declaration of destructuring. In the below example, we have declared the stud_name and stud_no. In the below example, we have declared two variables, stud_name and stud_age, and we are using the same independently.
Example:
Code:
println (stud_name)
println (stud_no)
After declaring the variable, we compile the same by using the following code.
Code:
val stud_name = stud.component1 ()
val stud_no = stud.component2 ()
In the above example, component1 and component2 are conventional principles widely used in kotlin. For returning the two values by using the function of the result object and some status, the compact way of doing the same is data class declaration and returning the instance. In the example below, data classes automatically declare the component function (N).
Code:
data class Result (val stud_name: String, val stud_no: Int)
fun student (...): Res {
return Res(stud_name, stud_no)
}
val (stud_name, stud_no) = function (...)
Output:
In the below example, we are creating the class as a student by using the attributes, and after, we are using the same class to print the values objects as follows.
Code:
fun main(args: Array<String>) {
val stud = Student("ABC", "student")
val (stud_name, stud_no) = stud
println("Name of "+stud_no+" is "+stud_name)
}
data class Student ( val p :String,val q: String ) {
var stud_name: String = p
var stud_no : String = q
}
Output:
Types of Kotlin Destructuring
Kotlin destructuring consists of the destructuring of an object into multiple variables, which is more convenient. The destructuring is working on the concept of a functional component. The variable number that was destructuring the declaration defined the class, which provided the number of components and functions starting from component1 and ending with components. By default, the data class in a kotlin implements the component’s function. The below example shows the return of the two values from the function by using kotlin destructuring as follows.
Code:
data class Data (val stud_name:String, val stud_no:Int)
fun sendData():Data {
return Data ("ABC", 101)
}
fun main (){
val obj = sendData()
println ("Stud_name is ${obj.stud_name}")
println ("Stud_no is ${obj.stud_no}")
val (stud_name, stud_no) = sendData()
println ("stud_name is $stud_name")
println ("stud_no is $stud_no")
}
Output:
In kotlin destructuring, the function definition is preceded by the operator keyword, used in a declaration of destructuring. Sometimes we ignore the declaration of variable destructuring in kotlin; to do the same, we need to put the underscore in the place of its name. Instead, we can use by using lambda parameters. Suppose the lambda parameter is a pair type declaring the function component; then, we can introduce the new parameter. The example below shows the type of destructuring declaration using lambda as follows.
Code:
fun main()
{
val stud = mutableMapOf<Int,String>()
stud.put (101, "ABC")
stud.put (102, "PQR")
stud.put (103, "XYZ")
stud.put (104,"CBZ")
println ("stud is")
println (stud)
val studmap = stud.mapValues { (key,value) -> "stud ${value}" }
println ("stud values")
println (studmap)
}
Output:
Kotlin Restructuring Array
Using kotlin array destructuring containing many values, we can destructure the multiple values simultaneously. Moreover, it provides a convenient way to destructure the object into various variables. Below is the syntax of kotlin array destructuring as follows.
Syntax:
val (var1, var2) = obj
Below is an example of an array as follows:
Code:
fun main() {
val arr = arrayOf ("11", "13", "15", "17", "19", "21")
val (no1, no2, no3, no4, no5) = arr
println (no1)
println (no2)
println (no3)
println (no4)
println (no5)
}
Output:
It provides an efficient way to create and initialize multiple variables simultaneously. It will get values from the member object and assigning into several variables. In the below example, we are destructuring only five elements.
Code:
fun main() {
val arr = arrayOf("11", "22", "33", "44", "55", "66")
val (num1, num2, num3, num4, num5, num6) = arr
println(num1)
println(num2)
println(num3)
println(num4)
println(num5)
println(num6)
}
Output:
If suppose we want to skip a variable element, we use an underscore instead of an element.
Code:
fun main() {
val de_arr = listOf("11", "13", "15", "17", "19", "21")
val (num1, _, _, _, num5, num6) = de_arr
println(num1)
println(num5)
println(num6)
}
Output:
Frequently Asked Questions(FAQ)
Other FAQs are mentioned below:
Q1. What is the syntax of kotlin destructuring?
Answer:
Below is the syntax; we can declare single or multiple variables using it.
val (var1, var2) = obj
Q2. What is a component function?
Answer:
In kotlin, we can use any object containing the component function. This is because the Kotlin compiler auto-generates the component functions for the data class.
Q3. How many values are returned by kotlin destructuring?
Answer:
It will return two or more values. First, we need to create the class instance for populating the value; then, we can return the instance from our function.
Conclusion
Using it is a convenient way to define local variables; we can define any local value. In it, multiple variables correspond to a class of properties associated with an instance. In addition, Kotlin provides programmers with a unique way of working with instances class with a destructuring declaration.
Recommended Articles
This is a guide to Kotlin Destructuring. Here we discuss the introduction and how to use destructuring declarations with Array and FAQ. You may also have a look at the following articles to learn more –