Collections Operations trong Kotlin
1. Giới thiệu
Kotlin cung cấp nhiều operations mạnh mẽ cho collections (List, Set, Map).
2. Transformation Operations
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
// map
println(numbers.map { it * 2 }) // [2, 4, 6, 8, 10]
// mapIndexed
println(numbers.mapIndexed { i, v -> "$i: $v" })
// flatMap
val nested = listOf(listOf(1, 2), listOf(3, 4))
println(nested.flatMap { it }) // [1, 2, 3, 4]
// flatten
println(nested.flatten()) // [1, 2, 3, 4]
}3. Filtering Operations
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6)
// filter
println(numbers.filter { it % 2 == 0 }) // [2, 4, 6]
// filterNot
println(numbers.filterNot { it % 2 == 0 }) // [1, 3, 5]
// filterNotNull
val withNull = listOf(1, null, 2, null, 3)
println(withNull.filterNotNull()) // [1, 2, 3]
// partition
val (evens, odds) = numbers.partition { it % 2 == 0 }
println("Even: $evens, Odd: $odds")
}4. Grouping Operations
data class Person(val name: String, val city: String)
fun main() {
val people = listOf(
Person("Alice", "NYC"),
Person("Bob", "LA"),
Person("Charlie", "NYC")
)
// groupBy
val byCity = people.groupBy { it.city }
println(byCity)
// groupingBy + eachCount
val words = listOf("a", "abc", "ab", "def", "ab")
val counts = words.groupingBy { it }.eachCount()
println(counts)
}5. Aggregate Operations
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.sum()) // 15
println(numbers.average()) // 3.0
println(numbers.count()) // 5
println(numbers.max()) // 5
println(numbers.min()) // 1
// reduce
println(numbers.reduce { acc, n -> acc + n }) // 15
// fold
println(numbers.fold(10) { acc, n -> acc + n }) // 25
}6. Element Operations
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers.first()) // 1
println(numbers.last()) // 5
println(numbers.firstOrNull { it > 10 }) // null
println(numbers.find { it > 3 }) // 4
println(numbers.findLast { it < 4 }) // 3
println(numbers.elementAt(2)) // 3
println(numbers.elementAtOrNull(10)) // null
}7. Ordering Operations
data class Person(val name: String, val age: Int)
fun main() {
val people = listOf(
Person("Bob", 30),
Person("Alice", 25),
Person("Charlie", 25)
)
// sortedBy
println(people.sortedBy { it.age })
// sortedByDescending
println(people.sortedByDescending { it.name })
// sortedWith (multiple criteria)
println(people.sortedWith(
compareBy({ it.age }, { it.name })
))
// reversed, shuffled
val nums = listOf(1, 2, 3)
println(nums.reversed())
println(nums.shuffled())
}8. Set Operations
fun main() {
val a = listOf(1, 2, 3, 4)
val b = listOf(3, 4, 5, 6)
println(a.union(b)) // [1, 2, 3, 4, 5, 6]
println(a.intersect(b)) // [3, 4]
println(a.subtract(b)) // [1, 2]
println(a.distinct()) // Remove duplicates
}9. Chunk và Window
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6, 7)
// chunked
println(numbers.chunked(3)) // [[1, 2, 3], [4, 5, 6], [7]]
// windowed
println(numbers.windowed(3)) // [[1, 2, 3], [2, 3, 4], [3, 4, 5], ...]
// windowed with step
println(numbers.windowed(3, 2)) // [[1, 2, 3], [3, 4, 5], [5, 6, 7]]
}10. Zip và Unzip
fun main() {
val names = listOf("Alice", "Bob", "Charlie")
val ages = listOf(25, 30, 35)
// zip
val combined = names.zip(ages)
println(combined) // [(Alice, 25), (Bob, 30), (Charlie, 35)]
// zip with transform
val people = names.zip(ages) { name, age ->
"$name is $age"
}
println(people)
// unzip
val (n, a) = combined.unzip()
println("Names: $n, Ages: $a")
}📝 Tóm tắt
- map, filter, flatMap cho transformations
- reduce, fold, sum, average cho aggregation
- sortedBy, sortedWith cho ordering
- first, find, elementAt cho element access
- chunked, windowed cho slicing
- zip, unzip cho combining lists
Last updated on