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

Collection Operations क्या हैं

Collection operations तैयार-बनी recipes हैं, जैसे sort या find, जो Kotlin आपको देता है ताकि आपको आम कामों के लिए loops न लिखने पड़ें।
Syntax
kotlin
list.sorted()
list.sortedDescending()
list.find { condition }
list.any { condition }
list.all { condition }
list.count { condition }
list.sum()

Sorting a Collection

.sorted() ascending natural order में elements वाली एक नई list return करता है, जबकि .sortedDescending() उन्हें descending order में return करता है -- कोई भी original list को modify नहीं करता।

उदाहरण: Sorting a Collection

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(5, 2, 8, 1)`
    val numbers = listOf(5, 2, 8, 1)
    // Print "Sorted: ${numbers.sorted()}" to the console, with a trailing newline
    println("Sorted: ${numbers.sorted()}")
    // Print "Descending: ${numbers.sortedDescending()}" to the console, with a trailing newline
    println("Descending: ${numbers.sortedDescending()}")
}

Searching with find, any, all, none

.find { predicate } किसी condition से मेल खाने वाला पहला element (या null) return करता है, जबकि .any, .all, और .none जांचते हैं कि क्रमशः कुछ, हर, या कोई element condition satisfy करता है या नहीं।

उदाहरण: Searching with find, any, all, none

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5)`
    val numbers = listOf(1, 2, 3, 4, 5)
    // Print "First even: ${numbers.find { it % 2 == 0 }}" to the console, with a trailing newline
    println("First even: ${numbers.find { it % 2 == 0 }}")
    // Print "Any negative: ${numbers.any { it < 0 }}" to the console, with a trailing newline
    println("Any negative: ${numbers.any { it < 0 }}")
    // Print "All positive: ${numbers.all { it > 0 }}" to the console, with a trailing newline
    println("All positive: ${numbers.all { it > 0 }}")
}

Counting and Summing

.count { predicate } गिनता है कि कितने elements किसी condition को satisfy करते हैं, जबकि .sum() numbers के किसी collection को सीधे जोड़ता है और .sumBy { } हर element से एक computed Int value sum करता है।

उदाहरण: Counting and Summing

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5, 6)`
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    // Print "Even count: ${numbers.count { it % 2 == 0 }}" to the console, with a trailing newline
    println("Even count: ${numbers.count { it % 2 == 0 }}")
    // Print "Sum: ${numbers.sum()}" to the console, with a trailing newline
    println("Sum: ${numbers.sum()}")
    // Print "Sum of squares: ${numbers.sumBy { it * it }}" to the console, with a trailing newline
    println("Sum of squares: ${numbers.sumBy { it * it }}")
}

Grouping Elements

.groupBy { } किसी collection को दी गई function के result से keyed एक Map में partition करता है, हर key के तहत मेल खाने वाले सभी elements को साथ इकट्ठा करते हुए।

उदाहरण: Grouping Elements

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `words`, set to `listOf("apple", "banana", "avocado", "blueberry", "cherry")`
    val words = listOf("apple", "banana", "avocado", "blueberry", "cherry")
    // Declare a read-only variable `grouped`, set to `words.groupBy { it.first() }`
    val grouped = words.groupBy { it.first() }
    // Print `grouped` to the console, with a trailing newline
    println(grouped)
}
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. किसी collection को sort या search करने के लिए manually for loop लिखना जब .sorted() या .find() जैसा एक साफ़ built-in function पहले से मौजूद हो।
  2. .sorted() (एक नई sorted list return करता है) को .sort() (किसी MutableList को in place sort करता है) के साथ confuse करना; गलत collection type पर गलत वाले का उपयोग करना।
  3. यह भूल जाना कि इनमें से कई operations original को in place modify करने के बजाय एक नई collection return करते हैं।
चैप्टर सारांश
  • .sorted()/.sortedDescending() ascending/descending order में sorted एक नई list return करते हैं।
  • .find { } पहला matching element या null return करता है; .any { }/.all { }/.none { } पूरी collection में conditions जांचते हैं।
  • .count { } matching elements गिनता है; .sum()/.sumBy { } numeric values जोड़ते हैं।
  • इनमें से ज़्यादातर operations original को mutate करने के बजाय एक नई collection या value return करते हैं।
🔒

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.