Collection Operations क्या हैं
In this page:
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
// 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()}")
}
Login to try C/C++/Java/PHP code in the editor
Searching with find, any, all, none
.find { predicate } किसी condition से मेल खाने वाला पहला element (या null) return करता है, जबकि .any, .all, और .none जांचते हैं कि क्रमशः कुछ, हर, या कोई element condition satisfy करता है या नहीं।
उदाहरण: Searching with find, any, all, none
// 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 }}")
}
Login to try C/C++/Java/PHP code in the editor
Counting and Summing
.count { predicate } गिनता है कि कितने elements किसी condition को satisfy करते हैं, जबकि .sum() numbers के किसी collection को सीधे जोड़ता है और .sumBy { } हर element से एक computed Int value sum करता है।
उदाहरण: Counting and Summing
// 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 }}")
}
Login to try C/C++/Java/PHP code in the editor
Grouping Elements
.groupBy { } किसी collection को दी गई function के result से keyed एक Map में partition करता है, हर key के तहत मेल खाने वाले सभी elements को साथ इकट्ठा करते हुए।
उदाहरण: Grouping Elements
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- किसी collection को sort या search करने के लिए manually
forloop लिखना जब.sorted()या.find()जैसा एक साफ़ built-in function पहले से मौजूद हो। .sorted()(एक नई sorted list return करता है) को.sort()(किसीMutableListको in place sort करता है) के साथ confuse करना; गलत collection type पर गलत वाले का उपयोग करना।- यह भूल जाना कि इनमें से कई operations original को in place modify करने के बजाय एक नई collection return करते हैं।
.sorted()/.sortedDescending()ascending/descending order में sorted एक नई list return करते हैं।.find { }पहला matching element याnullreturn करता है;.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: