Pablo
11/08/2024, 6:10 PMfor (item in items) { }ephemient
11/08/2024, 6:12 PMitems iteration orderephemient
11/08/2024, 6:12 PMBob
11/08/2024, 7:45 PMephemient
11/08/2024, 9:11 PMList iterates in order, whether you use `for`/`forEach`/`forEachIndexed`, so choose whatever is appropriate for the task at hand.
but forEachIndexed will give you an index on anything, even things which are not indexable, such as Set. do not use it without reason.Bob
11/08/2024, 9:21 PMPablo
11/10/2024, 7:27 PMfor (item in items) { } ... will it respect the order in items?ephemient
11/10/2024, 7:30 PMitemsPablo
11/10/2024, 7:34 PMPablo
11/10/2024, 7:34 PMPablo
11/10/2024, 7:34 PMPablo
11/10/2024, 7:34 PMephemient
11/10/2024, 7:34 PMitems is a List then it is ordered. if items is a Set then it may or may not have a consistent order.Pablo
11/10/2024, 7:35 PMPablo
11/10/2024, 7:35 PMPablo
11/10/2024, 7:35 PMPablo
11/10/2024, 7:35 PMKahloun Hamdi
11/11/2024, 12:51 PM// Ordered collection: List
val orderedItems = listOf("apple", "banana", "cherry")
for (item in orderedItems) {
println(item) // Output: apple, banana, cherry (in order)
}
// Unordered collection: HashSet
val unorderedItems = hashSetOf("apple", "banana", "cherry")
for (item in unorderedItems) {
println(item) // Output: No guaranteed order, may vary
}