← Back to Kotlin Course | Chapter 8: Collections | Lesson 6 of 7

Destructuring Declarations क्या हैं

Destructuring आपको एक object से कई values को एक single line में अलग named variables में unpack करने देता है।
Syntax
kotlin
val (a, b) = pair
val (prop1, prop2) = dataClassInstance

for ((key, value) in map) {
    // use key and value
}

Destructuring a Pair

standard library का Pair type component1() और component2() प्रदान करता है, इसलिए इसे सीधे दो अलग variables में destructure किया जा सकता है।

उदाहरण: Destructuring a Pair

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `pair`, set to `"Kotlin" to 2011`
    val pair = "Kotlin" to 2011
    val (language, year) = pair
    // Print "$language was released in $year" to the console, with a trailing newline
    println("$language was released in $year")
}

Destructuring a Data Class

चूंकि data classes अपनी constructor properties के लिए अपने आप componentN() functions generate करती हैं, किसी भी data class instance को सीधे destructure किया जा सकता है।

उदाहरण: Destructuring a Data Class

markup
// Define a data class `Point`
data class Point(val x: Int, val y: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `point`, set to `Point(3, 4)`
    val point = Point(3, 4)
    val (x, y) = point
    // Print "x=$x, y=$y" to the console, with a trailing newline
    println("x=$x, y=$y")
}

Destructuring in for Loops

Destructuring किसी for loop header के अंदर स्वाभाविक रूप से काम करता है, जो किसी Map की entries को key-value pairs के रूप में iterate करते समय खासतौर पर सुविधाजनक है।

उदाहरण: Destructuring in for Loops

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `scores`, set to `mapOf("Ana" to 90, "Bo" to 85)`
    val scores = mapOf("Ana" to 90, "Bo" to 85)
    for ((name, score) in scores) {
        // Print "$name scored $score" to the console, with a trailing newline
        println("$name scored $score")
    }
}

Skipping Components with Underscore

जब किसी destructured value के सिर्फ कुछ components चाहिए हों, तो _ उन components के लिए एक placeholder के रूप में उपयोग किया जा सकता है जिन्हें आप नज़रअंदाज़ करना चाहते हैं।

उदाहरण: Skipping Components with Underscore

markup
// Define a data class `Point3D`
data class Point3D(val x: Int, val y: Int, val z: Int)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `point`, set to `Point3D(1, 2, 3)`
    val point = Point3D(1, 2, 3)
    val (x, _, z) = point
    // Print "x=$x, z=$z" to the console, with a trailing newline
    println("x=$x, z=$z")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी regular class को destructure करने की कोशिश करना जिसने componentN() functions declare नहीं किए हैं (या जो data class नहीं है), जिसे compiler reject कर देता है।
  2. यह मान लेना कि destructuring order property names पर आधारित है; यह असल में component1(), component2(), आदि के ज़रिए declaration order पर आधारित है।
  3. अनावश्यक रूप से verbosely _ उपयोग करना बजाय underscore placeholder से किसी अवांछित component को skip करने के।
चैप्टर सारांश
  • val (a, b) = pair किसी object को उसके component1(), component2(), आदि functions का उपयोग करके अलग variables में destructure करता है।
  • Data classes अपनी constructor properties के लिए अपने आप componentN() functions प्रदान करती हैं, उन्हें out of the box destructurable बनाते हुए।
  • Pair और Map.Entry standard library types हैं जो पहले से destructuring support करते हैं।
  • एक underscore _ किसी ऐसे component की जगह उपयोग किया जा सकता है जिसे आप skip करना चाहते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.