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

Set और Map

एक Set एक bag है जो एक जैसा item दो बार रखने से मना करता है, और एक Map एक dictionary जैसा है जो हर key को बिल्कुल एक value से मिलाता है।
Syntax
kotlin
val set = setOf(a, b, c)                  // no duplicates
val map = mapOf(key1 to value1, key2 to value2)
map[key]                                  // null if missing
map.getOrDefault(key, defaultValue)

Creating and Using a Set

setOf(...) एक collection बनाता है जो duplicate values को अपने आप हटा देता है, तब उपयोगी जब uniqueness order या position से ज़्यादा मायने रखे।

उदाहरण: Creating and Using a Set

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `uniqueNumbers`, set to `setOf(1, 2, 2, 3, 3, 3)`
    val uniqueNumbers = setOf(1, 2, 2, 3, 3, 3)
    // Print `uniqueNumbers` to the console, with a trailing newline
    println(uniqueNumbers)
    // Print "Contains 2: ${uniqueNumbers.contains(2)}" to the console, with a trailing newline
    println("Contains 2: ${uniqueNumbers.contains(2)}")
}

Creating and Using a Map

mapOf(...) key-value pairs का एक read-only collection बनाता है, हर key को उसकी value से जोड़ने के लिए infix to function का उपयोग करते हुए।

उदाहरण: Creating and Using a Map

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `ages`, set to `mapOf("Ana" to 30, "Bo" to 25)`
    val ages = mapOf("Ana" to 30, "Bo" to 25)
    // Print `ages` to the console, with a trailing newline
    println(ages)
    // Print "Ana's age: ${ages["Ana"]}" to the console, with a trailing newline
    println("Ana's age: ${ages["Ana"]}")
}

Handling Missing Map Keys

map[key] से किसी गायब key को ढूंढना throw करने के बजाय null return करता है, इसलिए result स्वाभाविक रूप से एक nullable type है जिसे ?: या एक null check से handle करना चाहिए।

उदाहरण: Handling Missing Map Keys

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `ages`, set to `mapOf("Ana" to 30)`
    val ages = mapOf("Ana" to 30)
    // Declare a read-only variable `boAge`, set to `ages["Bo"] ?: -1`
    val boAge = ages["Bo"] ?: -1
    // Print "Bo's age (or -1 if missing): $boAge" to the console, with a trailing newline
    println("Bo's age (or -1 if missing): $boAge")
}

Mutable Sets and Maps

mutableSetOf() और mutableMapOf() उनके mutable counterparts बनाते हैं, बनने के बाद sets के लिए .add() और maps के लिए .put()/map[key] = value को support करते हुए।

उदाहरण: Mutable Sets and Maps

markup
fun main() {
    val tags = mutableSetOf("kotlin", "jvm")
    tags.add("android")
    tags.add("kotlin") // duplicate, ignored

    val scores = mutableMapOf("Ana" to 90)
    scores["Bo"] = 85

    println(tags)
    println(scores)
}
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. यह उम्मीद करना कि एक Set किसी List की तरह insertion order बनाए रखेगा; एक साधारण HashSet-backed setOf() order की गारंटी नहीं देता (हालांकि LinkedHashSet देता है)।
  2. किसी गायब key के लिए Map पर square-bracket access उपयोग करना और exception के बजाय अप्रत्याशित रूप से null पाना, फिर उस null को handle न करना।
  3. mapOf() (read-only) को mutableMapOf() के साथ confuse करना जब map को असल में बनने के बाद modify करना हो।
चैप्टर सारांश
  • setOf(...) एक read-only Set बनाता है जो duplicate values को अपने आप हटा देता है।
  • mapOf(...) key-value pairs का एक read-only Map बनाता है, key to value syntax का उपयोग करके बनाया गया।
  • Map में किसी गायब key को map[key] से access करना throw करने के बजाय null return करता है।
  • mutableSetOf() और mutableMapOf() mutable counterparts हैं जो .add()/.put() और removal को support करते हैं।
🔒

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.