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

Sequences क्या हैं

एक Sequence पूरे group के लिए एक पूरा step खत्म करने के बजाय, अगले item पर जाने से पहले हर step से एक समय में एक item process करता है।
Syntax
kotlin
val result = collection.asSequence()
    .map { it transformation }
    .filter { condition }
    .toList()            // terminal operation runs the pipeline

Creating a Sequence

किसी collection पर .asSequence() call करना इसे एक Sequence में बदल देता है, जो हर step पर eagerly intermediate lists बनाने के बजाय elements को lazily process करता है।

उदाहरण: Creating a Sequence

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `(1..5).asSequence()`
    val numbers = (1..5).asSequence()
    // Print `numbers.toList()` to the console, with a trailing newline
    println(numbers.toList())
}

Lazy Evaluation with map and filter

किसी sequence पर .map { } और .filter { } chain करना बस operations की एक pipeline बना देता है -- जब तक कोई terminal operation call न हो तब तक असल में कुछ नहीं चलता।

उदाहरण: Lazy Evaluation with map and filter

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `result`, set to `listOf(1, 2, 3, 4, 5)`
    val result = listOf(1, 2, 3, 4, 5)
        .asSequence()
        .map { println("Mapping $it"); it * 2 }
        .filter { it > 4 }
        .toList()
    // Print "Result: $result" to the console, with a trailing newline
    println("Result: $result")
}

Terminal Operations Trigger Execution

.toList(), .first(), .count(), या .sum() जैसे functions terminal operations हैं -- एक को call करना ही असल में पूरी lazy pipeline को चलाता है और एक concrete result produce करता है।

उदाहरण: Terminal Operations Trigger Execution

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `firstBigSquare`, set to `(1..100).asSequence()`
    val firstBigSquare = (1..100).asSequence()
        .map { it * it }
        .first { it > 50 }
    // Print "First square over 50: $firstBigSquare" to the console, with a trailing newline
    println("First square over 50: $firstBigSquare")
}

Sequences vs Regular Collections

.map() जैसे Regular List operations हर step पर eagerly एक पूरी नई list बनाते हैं, जबकि एक Sequence अगले element पर जाने से पहले हर element को पूरी pipeline से process करता है -- बड़े data पर लंबी chains के लिए उपयोगी।

Note: छोटी collections के लिए, साधारण list operations आमतौर पर सरल और काफी तेज़ हैं; sequences बड़े data या कई chained steps के साथ चमकते हैं।

उदाहरण: Sequences vs Regular Collections

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `eagerResult`, set to `(1..5).map { it * 2 }.filter { it > 4 }`
    val eagerResult = (1..5).map { it * 2 }.filter { it > 4 }
    // Declare a read-only variable `lazyResult`, set to `(1..5).asSequence().map { it * 2 }.filter { it > 4 }.toList()`
    val lazyResult = (1..5).asSequence().map { it * 2 }.filter { it > 4 }.toList()
    // Print "Eager: $eagerResult" to the console, with a trailing newline
    println("Eager: $eagerResult")
    // Print "Lazy: $lazyResult" to the console, with a trailing newline
    println("Lazy: $lazyResult")
}
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. किसी बड़े dataset पर .map().filter() की एक regular List chain का उपयोग करना और अनावश्यक intermediate lists से हैरान होना; एक Sequence lazy होकर इससे बचता है।
  2. किसी sequence chain के आखिर में .toList() (या कोई दूसरा terminal operation) call करना भूल जाना, क्योंकि intermediate operations lazy हैं और किसी terminal के invoke होने तक नहीं चलते।
  3. यह मान लेना कि sequences हमेशा तेज़ होते हैं; छोटी collections के लिए, किसी sequence बनाने का overhead साधारण list operations की तुलना में फायदे से ज़्यादा हो सकता है।
चैप्टर सारांश
  • asSequence() किसी collection को एक Sequence में बदल देता है, जो operations को lazily, element-by-element evaluate करता है।
  • किसी sequence पर intermediate operations (map, filter) एक pipeline बना देते हैं लेकिन जब तक कोई terminal operation call न हो तब तक नहीं चलते।
  • .toList(), .first(), या .sum() जैसे terminal operations किसी sequence की असली processing को trigger करते हैं।
  • Sequences हर step पर intermediate collections बनाने से बचते हैं, जो बड़े data या लंबी operation chains के लिए ज़्यादा efficient हो सकता है।
🔒

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.