Kotlin List 集合 #

一、创建 List #

1.1 只读 List #

kotlin
// 使用 listOf
val list = listOf(1, 2, 3, 4, 5)

// 空列表
val empty = emptyList<Int>()

// 使用 List 构造函数
val list2 = List(5) { it * 2 }  // [0, 2, 4, 6, 8]

1.2 可变 List #

kotlin
// 使用 mutableListOf
val mutable = mutableListOf(1, 2, 3)

// 使用 ArrayList
val arrayList = arrayListOf(1, 2, 3)

// 使用构造函数
val mutable2 = MutableList(5) { it }  // [0, 1, 2, 3, 4]

二、访问元素 #

2.1 按索引访问 #

kotlin
val list = listOf("A", "B", "C", "D", "E")

list[0]           // "A"
list.get(0)       // "A"
list.getOrNull(10)  // null
list.getOrElse(10) { "Default" }  // "Default"

2.2 获取首尾元素 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.first()           // 1
list.last()            // 5
list.firstOrNull()     // 1
list.lastOrNull()      // 5

// 条件获取
list.first { it > 2 }  // 3
list.last { it < 4 }   // 3

2.3 随机元素 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.random()          // 随机元素
list.randomOrNull()    // 随机元素或 null

三、添加元素 #

3.1 添加单个元素 #

kotlin
val list = mutableListOf(1, 2, 3)

list.add(4)           // 添加到末尾
list.add(0, 0)        // 在索引 0 处插入
// [0, 1, 2, 3, 4]

3.2 添加多个元素 #

kotlin
val list = mutableListOf(1, 2, 3)

list.addAll(listOf(4, 5, 6))
list.addAll(0, listOf(-1, 0))
// [-1, 0, 1, 2, 3, 4, 5, 6]

3.3 使用 + 操作符 #

kotlin
val list = listOf(1, 2, 3)

val newList = list + 4       // [1, 2, 3, 4]
val newList2 = list + listOf(4, 5)  // [1, 2, 3, 4, 5]

四、删除元素 #

4.1 按索引删除 #

kotlin
val list = mutableListOf(1, 2, 3, 4, 5)

list.removeAt(0)       // 删除索引 0 的元素
// [2, 3, 4, 5]

4.2 按值删除 #

kotlin
val list = mutableListOf(1, 2, 3, 2, 4)

list.remove(2)         // 删除第一个 2
// [1, 3, 2, 4]

4.3 条件删除 #

kotlin
val list = mutableListOf(1, 2, 3, 4, 5)

list.removeAll { it > 3 }  // 删除所有大于 3 的元素
// [1, 2, 3]

list.retainAll { it > 1 }  // 保留大于 1 的元素
// [2, 3]

4.4 清空列表 #

kotlin
val list = mutableListOf(1, 2, 3)

list.clear()
// []

4.5 使用 - 操作符 #

kotlin
val list = listOf(1, 2, 3, 2, 4)

val newList = list - 2        // [1, 3, 2, 4]
val newList2 = list - listOf(2, 3)  // [1, 4]

五、修改元素 #

5.1 按索引修改 #

kotlin
val list = mutableListOf(1, 2, 3, 4, 5)

list[0] = 10           // [10, 2, 3, 4, 5]
list.set(1, 20)        // [10, 20, 3, 4, 5]

5.2 批量修改 #

kotlin
val list = mutableListOf(1, 2, 3, 4, 5)

list.fill(0)           // [0, 0, 0, 0, 0]

六、子列表 #

6.1 获取子列表 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.subList(1, 4)     // [2, 3, 4]
list.slice(1..3)       // [2, 3, 4]
list.slice(listOf(0, 2, 4))  // [1, 3, 5]

6.2 take/drop #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.take(3)           // [1, 2, 3]
list.takeLast(3)       // [3, 4, 5]
list.drop(2)           // [3, 4, 5]
list.dropLast(2)       // [1, 2, 3]

6.3 条件截取 #

kotlin
val list = listOf(1, 2, 3, 4, 5, 1, 2)

list.takeWhile { it < 4 }    // [1, 2, 3]
list.dropWhile { it < 3 }    // [3, 4, 5, 1, 2]

七、查找元素 #

7.1 查找索引 #

kotlin
val list = listOf(1, 2, 3, 2, 4)

list.indexOf(2)        // 1
list.lastIndexOf(2)    // 3
list.indexOfFirst { it > 2 }  // 2
list.indexOfLast { it < 4 }   // 3

7.2 查找元素 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.find { it > 3 }        // 4
list.findLast { it < 4 }    // 3
list.findOrNull { it > 10 } // null

7.3 包含判断 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.contains(3)            // true
3 in list                   // true
list.containsAll(listOf(1, 2))  // true

八、排序 #

8.1 排序方法 #

kotlin
val list = mutableListOf(3, 1, 4, 1, 5, 9, 2, 6)

// 原地排序
list.sort()             // [1, 1, 2, 3, 4, 5, 6, 9]
list.sortDescending()   // [9, 6, 5, 4, 3, 2, 1, 1]

// 返回新列表
val sorted = list.sorted()
val sortedDesc = list.sortedDescending()

8.2 按条件排序 #

kotlin
data class Person(val name: String, val age: Int)

val people = listOf(
    Person("Bob", 30),
    Person("Alice", 25),
    Person("Charlie", 35)
)

val sorted = people.sortedBy { it.age }
val sortedDesc = people.sortedByDescending { it.age }

// 多条件排序
val multiSorted = people.sortedWith(
    compareBy({ it.age }, { it.name })
)

8.3 反转 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.reversed()         // [5, 4, 3, 2, 1]

val mutable = mutableListOf(1, 2, 3)
mutable.reverse()       // [3, 2, 1]

九、去重 #

9.1 基本去重 #

kotlin
val list = listOf(1, 2, 2, 3, 3, 3, 4)

list.distinct()         // [1, 2, 3, 4]
list.toSet()            // {1, 2, 3, 4}

9.2 按条件去重 #

kotlin
data class Person(val name: String, val age: Int)

val people = listOf(
    Person("Alice", 25),
    Person("Bob", 30),
    Person("Alice", 35)
)

people.distinctBy { it.name }
// [Person(Alice, 25), Person(Bob, 30)]

十、其他操作 #

10.1 合并 #

kotlin
val list1 = listOf(1, 2, 3)
val list2 = listOf(4, 5, 6)

list1 + list2           // [1, 2, 3, 4, 5, 6]
list1.zip(list2)        // [(1, 4), (2, 5), (3, 6)]

10.2 分组 #

kotlin
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

list.chunked(3)         // [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
list.windowed(3)        // [[1, 2, 3], [2, 3, 4], ...]

10.3 统计 #

kotlin
val list = listOf(1, 2, 3, 4, 5)

list.sum()              // 15
list.average()          // 3.0
list.maxOrNull()        // 5
list.minOrNull()        // 1
list.count()            // 5
list.count { it > 2 }   // 3

十一、实战示例 #

11.1 列表去重保持顺序 #

kotlin
fun <T> List<T>.distinctPreserveOrder(): List<T> {
    return this.toMutableList().also { list ->
        val seen = mutableSetOf<T>()
        val iterator = list.iterator()
        while (iterator.hasNext()) {
            val element = iterator.next()
            if (!seen.add(element)) {
                iterator.remove()
            }
        }
    }
}

11.2 分组统计 #

kotlin
val words = listOf("apple", "banana", "cherry", "date", "elderberry")

val byLength = words.groupBy { it.length }
// {5=[apple], 6=[banana, cherry], 4=[date], 10=[elderberry]}

val countByLength = words.groupingBy { it.length }.eachCount()
// {5=1, 6=2, 4=1, 10=1}

十二、总结 #

List 常用操作:

操作 方法
访问 get, first, last
添加 add, addAll
删除 remove, removeAt
查找 find, indexOf
排序 sort, sortedBy
去重 distinct

下一步,让我们学习 Set集合

最后更新:2026-03-27