Set क्या है
एक Set एक bag है जिसमें कभी duplicates नहीं होते, जैसे एक sticker collection जहां आप हर एक की सिर्फ एक copy रखते हैं। पहले से मौजूद चीज़ जोड़ने से कुछ नहीं बदलता।
In this page:
Syntax
val set = Set(value1, value2, value3)
val bigger = set + value
set.contains(value)
Set
एक Set unique elements को बिना किसी तय order और बिना duplicates के store करता है -- पहले से मौजूद element जोड़ने का कोई असर नहीं होता।
Set(1, 2, 3) default रूप से एक immutable set बनाता है; scala.collection.mutable.Set +=/-= को support करने वाला एक mutable version देता है। Sets membership tests और data से duplicates हटाने के लिए आदर्श हैं।
Note:
किसी Set में duplicate value जोड़ना चुपचाप कुछ नहीं करता -- पहले से मौजूद element के लिए set का size कभी नहीं बदलता।
उदाहरण: Set
object Main extends App {
val ids = Set(1, 2, 3)
val expanded = ids + 2 + 4 // 2 already exists, ignored
println(expanded)
println(expanded.contains(3))
println(expanded.size)
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- यह उम्मीद करना कि एक
Setinsertion order बनाए रखेगा या duplicates की अनुमति देगा, जबकि duplicates हट जाते हैं और order की गारंटी नहीं है। ids + 4का उपयोग करना औरidsके बदलने की उम्मीद करना, जबकि यह एक नयाSetreturn करता है क्योंकि default वाला immutable है।ids(0)जैसे किसी set को index करना, जिसका मतलब पहला element नहीं बल्कि यह जांचना है कि0set में है या नहीं।
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: