Set और Map
In this page:
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
// 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)}")
}
Login to try C/C++/Java/PHP code in the editor
Creating and Using a Map
mapOf(...) key-value pairs का एक read-only collection बनाता है, हर key को उसकी value से जोड़ने के लिए infix to function का उपयोग करते हुए।
उदाहरण: Creating and Using a Map
// 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"]}")
}
Login to try C/C++/Java/PHP code in the editor
Handling Missing Map Keys
map[key] से किसी गायब key को ढूंढना throw करने के बजाय null return करता है, इसलिए result स्वाभाविक रूप से एक nullable type है जिसे ?: या एक null check से handle करना चाहिए।
उदाहरण: Handling Missing Map Keys
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Mutable Sets and Maps
mutableSetOf() और mutableMapOf() उनके mutable counterparts बनाते हैं, बनने के बाद sets के लिए .add() और maps के लिए .put()/map[key] = value को support करते हुए।
उदाहरण: Mutable Sets and Maps
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)
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक
SetकिसीListकी तरह insertion order बनाए रखेगा; एक साधारणHashSet-backedsetOf()order की गारंटी नहीं देता (हालांकिLinkedHashSetदेता है)। - किसी गायब key के लिए
Mapपर square-bracket access उपयोग करना और exception के बजाय अप्रत्याशित रूप सेnullपाना, फिर उस null को handle न करना। mapOf()(read-only) कोmutableMapOf()के साथ confuse करना जब map को असल में बनने के बाद modify करना हो।
setOf(...)एक read-onlySetबनाता है जो duplicate values को अपने आप हटा देता है।mapOf(...)key-value pairs का एक read-onlyMapबनाता है,key to valuesyntax का उपयोग करके बनाया गया।Mapमें किसी गायब key कोmap[key]से access करना throw करने के बजायnullreturn करता है।mutableSetOf()औरmutableMapOf()mutable counterparts हैं जो.add()/.put()और removal को support करते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: