/** * 第 7 章 · 集合 & 函数式操作 — 综合示例 * * 涵盖:map/filter/reduce/groupBy/partition/zip/chunked/windowed/Sequence */ data class Order( val id: String, val userId: String, val amount: Double, val items: List, val status: String, ) fun main() { println("=== 1. 创建集合 ===") val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val mset = mutableSetOf("a", "b", "c") val map = mapOf("x" to 1, "y" to 2) println("list = $list") println("set = $mset") println("map = $map") println("\n=== 2. 转换 ===") println("map { it * 2 } = ${list.map { it * 2 }}") println("mapIndexed = ${list.mapIndexed { i, v -> "[${i}]=${v}" }}") println("flatMap = ${list.take(3).flatMap { listOf(it, it * 10) }}") println("\n=== 3. 筛选 ===") println("偶数: ${list.filter { it % 2 == 0 }}") val (even, odd) = list.partition { it % 2 == 0 } println("partition: even=$even, odd=$odd") println("take(3) = ${list.take(3)}") println("drop(7) = ${list.drop(7)}") println("takeWhile{<5} = ${list.takeWhile { it < 5 }}") println("\n=== 4. 聚合 ===") println("sum = ${list.sum()}") println("average = ${list.average()}") println("count{>5} = ${list.count { it > 5 }}") println("reduce: 1+2+...+10 = ${list.reduce { acc, x -> acc + x }}") println("fold(100): 100 + 1+2+...+10 = ${list.fold(100) { acc, x -> acc + x }}") println("\n=== 5. 排序 ===") val users = listOf( "Charlie" to 25, "Alice" to 30, "Bob" to 20, ) println("按年龄: ${users.sortedBy { it.second }}") println("按名字倒序: ${users.sortedByDescending { it.first }}") println("\n=== 6. 分组 ===") val words = listOf("apple", "banana", "apricot", "blueberry", "cherry") println("groupBy 首字母:") words.groupBy { it.first() }.forEach { (k, v) -> println(" $k -> $v") } println("associateBy 首字母(重复 key 后者覆盖):") println(" ${words.associateBy { it.first() }}") println("associateWith 长度:") println(" ${words.associateWith { it.length }}") println("\n=== 7. zip / unzip / chunked / windowed ===") val names = listOf("a", "b", "c") val ages = listOf(1, 2, 3) println("zip: ${names.zip(ages)}") println("zip + transform: ${names.zip(ages) { n, a -> "$n=$a" }}") println("chunked(3): ${list.chunked(3)}") println("windowed(3): ${list.windowed(3).take(3)}") println("\n=== 8. Sequence vs List ===") println("--- List 急性 ---") listOf(1, 2, 3, 4, 5) .filter { print("F$it "); it % 2 == 0 } .map { print("M$it "); it * 2 } .first() println() println("--- Sequence 惰性 ---") listOf(1, 2, 3, 4, 5).asSequence() .filter { print("F$it "); it % 2 == 0 } .map { print("M$it "); it * 2 } .first() println() println("👆 Sequence 在找到第一个就停了!") println("\n=== 9. 实战:电商订单分析 ===") val orders = listOf( Order("o001", "u1", 100.0, listOf("apple"), "PAID"), Order("o002", "u1", 50.0, listOf("banana", "cherry"), "PAID"), Order("o003", "u2", 200.0, listOf("apple", "banana"), "REFUND"), Order("o004", "u2", 80.0, listOf("apple"), "PAID"), Order("o005", "u3", 30.0, listOf("cherry"), "CANCEL"), Order("o006", "u3", 150.0, listOf("banana"), "PAID"), ) val revenue = orders.filter { it.status == "PAID" }.sumOf { it.amount } println("总收入: ¥$revenue") println("\n按用户统计已付金额:") orders .filter { it.status == "PAID" } .groupBy { it.userId } .mapValues { (_, list) -> list.sumOf { it.amount } } .toList() .sortedByDescending { it.second } .forEach { (uid, total) -> println(" $uid: ¥$total") } println("\n商品销量 Top 3:") orders .flatMap { it.items } .groupingBy { it } .eachCount() .toList() .sortedByDescending { it.second } .take(3) .forEach { (item, cnt) -> println(" $item: $cnt 单") } val (paid, others) = orders.partition { it.status == "PAID" } println("\n已付订单数: ${paid.size} / 其他: ${others.size}") }