Kotlin第五阶段学习内容详情

Kotlin 第五阶段学习内容详情:集合与常用标准库

本阶段目标是掌握 Kotlin 中最常用的集合类型和集合操作函数。集合是日常开发高频内容,学会后你可以更自然地处理学生列表、商品列表、订单列表等数据。

建议用时:4-5 天。

阶段目标

  • 能区分 ListSetMap
  • 能区分只读集合和可变集合。
  • 能使用 filtermapforEach
  • 能使用 findgroupBysortedBy
  • 能理解 reducefold 的基本用法。
  • 能用集合操作完成数据筛选、转换、统计和分组。

第 1 天:集合基础

1. List

List 是有序集合,可以有重复元素。

fun main() {
    val names = listOf("Alice", "Bob", "Alice")

    println(names[0])
    println(names.size)
}

2. MutableList

MutableList 可以增删改。

fun main() {
    val names = mutableListOf("Alice", "Bob")

    names.add("Cindy")
    names.remove("Bob")
    names[0] = "Alex"

    println(names)
}

3. Set

Set 不允许重复元素。

fun main() {
    val numbers = setOf(1, 2, 2, 3)
    println(numbers)
}

4. Map

Map 保存键值对。

fun main() {
    val scores = mapOf(
        "Alice" to 95,
        "Bob" to 80
    )

    println(scores["Alice"])
}

5. MutableMap

fun main() {
    val scores = mutableMapOf("Alice" to 95)

    scores["Bob"] = 80
    scores["Alice"] = 98

    println(scores)
}

第 2 天:遍历集合

1. for 遍历

fun main() {
    val names = listOf("Alice", "Bob", "Cindy")

    for (name in names) {
        println(name)
    }
}

2. forEach

fun main() {
    val names = listOf("Alice", "Bob", "Cindy")

    names.forEach {
        println(it)
    }
}

3. 遍历 Map

fun main() {
    val scores = mapOf("Alice" to 95, "Bob" to 80)

    for ((name, score) in scores) {
        println("$name:$score")
    }
}

第 3 天:筛选与转换

1. filter

筛选符合条件的元素。

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val evenNumbers = numbers.filter { it % 2 == 0 }

    println(evenNumbers)
}

2. map

把每个元素转换成另一个结果。

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val squares = numbers.map { it * it }

    println(squares)
}

3. 链式调用

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)

    val result = numbers
        .filter { it % 2 == 0 }
        .map { it * it }

    println(result)
}

第 4 天:查找、排序与分组

1. find

data class User(val name: String, val age: Int)

fun main() {
    val users = listOf(
        User("Alice", 20),
        User("Bob", 17)
    )

    val adult = users.find { it.age >= 18 }
    println(adult)
}

2. sortedBy

data class Product(val name: String, val price: Int)

fun main() {
    val products = listOf(
        Product("键盘", 199),
        Product("鼠标", 99),
        Product("显示器", 899)
    )

    val sorted = products.sortedBy { it.price }
    println(sorted)
}

3. sortedByDescending

val sorted = products.sortedByDescending { it.price }

4. groupBy

data class User(val name: String, val city: String)

fun main() {
    val users = listOf(
        User("Alice", "杭州"),
        User("Bob", "上海"),
        User("Cindy", "杭州")
    )

    val grouped = users.groupBy { it.city }
    println(grouped)
}

第 5 天:统计与综合练习

1. sumOf

data class Order(val id: Long, val amount: Int)

fun main() {
    val orders = listOf(
        Order(1, 100),
        Order(2, 200),
        Order(3, 50)
    )

    val total = orders.sumOf { it.amount }
    println(total)
}

2. reduce

reduce 用于把集合元素逐步合并为一个结果。

fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val sum = numbers.reduce { acc, number -> acc + number }

    println(sum)
}

3. fold

foldreduce 类似,但可以提供初始值。

fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val sum = numbers.fold(10) { acc, number -> acc + number }

    println(sum)
}

综合练习

练习 1:筛选及格学生

data class Student(val name: String, val score: Int)

fun main() {
    val students = listOf(
        Student("Alice", 95),
        Student("Bob", 58),
        Student("Cindy", 76)
    )

    val passed = students.filter { it.score >= 60 }
    println(passed)
}

练习 2:商品价格排序

data class Product(val name: String, val price: Int)

fun main() {
    val products = listOf(
        Product("键盘", 199),
        Product("鼠标", 99),
        Product("显示器", 899)
    )

    val sorted = products.sortedBy { it.price }
    println(sorted)
}

练习 3:订单总金额

data class Order(val id: Long, val amount: Int)

fun main() {
    val orders = listOf(
        Order(1, 100),
        Order(2, 200),
        Order(3, 50)
    )

    val total = orders.sumOf { it.amount }
    println("订单总金额:$total")
}

练习 4:按城市分组用户

data class User(val name: String, val city: String)

fun main() {
    val users = listOf(
        User("Alice", "杭州"),
        User("Bob", "上海"),
        User("Cindy", "杭州")
    )

    val grouped = users.groupBy { it.city }
    println(grouped)
}

必须掌握清单

常见错误

1. 想修改只读集合

val names = listOf("Alice")
names.add("Bob") // 错误

应改为:

val names = mutableListOf("Alice")
names.add("Bob")

2. find 结果可能为空

val user = users.find { it.name == "Tom" }
println(user.name) // 错误

应改为:

println(user?.name ?: "用户不存在")

阶段复盘问题

  1. ListSetMap 有什么区别?
  2. ListMutableList 有什么区别?
  3. filtermap 分别用于什么场景?
  4. find 为什么返回可空结果?
  5. groupBy 的结果是什么结构?
  6. reducefold 有什么区别?
posted @ 2026-06-30 10:57  呢哇哦比较  阅读(3)  评论(0)    收藏  举报