集合概述 #

一、集合层次结构 #

1.1 主要集合类型 #

text
Iterable
├── Seq (序列)
│   ├── List
│   ├── Vector
│   ├── Array
│   └── String
├── Set (集合)
│   ├── HashSet
│   └── TreeSet
└── Map (映射)
    ├── HashMap
    └── TreeMap

1.2 集合特质 #

特质 说明
Iterable 可迭代集合的基特质
Seq 有序序列
Set 无序不重复集合
Map 键值对映射

二、可变与不可变 #

2.1 不可变集合(默认) #

scala
val list = List(1, 2, 3)
val set = Set(1, 2, 3)
val map = Map("a" -> 1, "b" -> 2)

不可变集合修改后返回新集合:

scala
val list1 = List(1, 2, 3)
val list2 = list1 :+ 4

println(list1)
println(list2)

2.2 可变集合 #

需要显式导入:

scala
import scala.collection.mutable

val buffer = mutable.ListBuffer(1, 2, 3)
buffer += 4

println(buffer)

2.3 可变与不可变对比 #

scala
import scala.collection.mutable

val immutableList = List(1, 2, 3)
val mutableBuffer = mutable.ListBuffer(1, 2, 3)

val newList = immutableList :+ 4
mutableBuffer += 4

println(s"Immutable: $immutableList")
println(s"New list: $newList")
println(s"Mutable: $mutableBuffer")

三、集合创建 #

3.1 List 创建 #

scala
val list1 = List(1, 2, 3)
val list2 = 1 :: 2 :: 3 :: Nil
val list3 = List.fill(5)(0)
val list4 = List.range(1, 10)
val list5 = List.tabulate(5)(n => n * n)

3.2 Set 创建 #

scala
val set1 = Set(1, 2, 3)
val set2 = Set(1, 1, 2, 2, 3)
val set3 = Set.empty[Int]

3.3 Map 创建 #

scala
val map1 = Map("a" -> 1, "b" -> 2)
val map2 = Map(("a", 1), ("b", 2))
val map3 = Map.empty[String, Int]

3.4 Vector 创建 #

scala
val vector1 = Vector(1, 2, 3)
val vector2 = Vector.fill(5)(0)
val vector3 = Vector.range(1, 10)

四、基本操作 #

4.1 通用操作 #

scala
val list = List(1, 2, 3, 4, 5)

list.isEmpty
list.nonEmpty
list.size
list.head
list.last
list.tail
list.init
list.take(3)
list.drop(2)
list.takeRight(2)
list.dropRight(2)

4.2 查找操作 #

scala
val list = List(1, 2, 3, 4, 5)

list.find(_ > 3)
list.filter(_ > 3)
list.filterNot(_ > 3)
list.exists(_ > 3)
list.forall(_ > 0)
list.count(_ > 3)

4.3 转换操作 #

scala
val list = List(1, 2, 3, 4, 5)

list.map(_ * 2)
list.flatMap(n => List(n, n * 2))
list.grouped(2).toList
list.sliding(3).toList

4.4 聚合操作 #

scala
val list = List(1, 2, 3, 4, 5)

list.sum
list.product
list.min
list.max
list.reduce(_ + _)
list.fold(0)(_ + _)
list.foldLeft(0)(_ + _)
list.foldRight(0)(_ + _)

五、集合转换 #

5.1 转为 List #

scala
val set = Set(1, 2, 3)
val map = Map("a" -> 1, "b" -> 2)

set.toList
map.toList
map.keys.toList
map.values.toList

5.2 转为 Set #

scala
val list = List(1, 2, 2, 3, 3, 3)

list.toSet

5.3 转为 Map #

scala
val list = List(("a", 1), ("b", 2))

list.toMap

val list2 = List("a", "b", "c")
list2.map(s => s -> s.length).toMap

5.4 转为数组 #

scala
val list = List(1, 2, 3)

list.toArray

六、集合比较 #

6.1 List vs Vector #

特性 List Vector
头部添加 O(1) O(1)
尾部添加 O(n) O(1)
随机访问 O(n) O(log n)
适用场景 队列、栈 随机访问

6.2 选择指南 #

场景 推荐集合
顺序访问 List
随机访问 Vector, Array
去重 Set
键值存储 Map
频繁修改 mutable 集合

七、集合与函数式编程 #

7.1 链式操作 #

scala
val result = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  .filter(_ % 2 == 0)
  .map(_ * 2)
  .take(3)
  .sum

7.2 for 推导式 #

scala
val result = for
  x <- List(1, 2, 3)
  y <- List(10, 20)
yield x + y

val result2 = for
  x <- List(1, 2, 3, 4, 5)
  if x % 2 == 0
yield x * x

7.3 惰性求值 #

scala
val lazyView = List(1, 2, 3, 4, 5).view
  .map { x => println(s"Processing $x"); x * 2 }
  .filter { x => println(s"Filtering $x"); x > 4 }

val result = lazyView.take(2).toList

八、并行集合 #

8.1 创建并行集合 #

scala
val parList = List(1, 2, 3, 4, 5).par

val result = parList.map(_ * 2).sum

8.2 并行操作 #

scala
import scala.collection.parallel.CollectionConverters._

val result = (1 to 1000000).par
  .filter(_ % 2 == 0)
  .map(_ * 2)
  .sum

九、集合最佳实践 #

9.1 优先使用不可变集合 #

scala
val list = List(1, 2, 3)
val newList = list :+ 4

9.2 选择合适的集合类型 #

scala
val queue = List(1, 2, 3)
val randomAccess = Vector(1, 2, 3)
val unique = Set(1, 2, 3)
val lookup = Map("a" -> 1, "b" -> 2)

9.3 使用 view 优化性能 #

scala
val result = largeCollection.view
  .map(expensiveOperation)
  .filter(predicate)
  .take(10)
  .toList

十、总结 #

集合层次 #

类型 特点
Seq 有序,可重复
Set 无序,不重复
Map 键值对

可变性 #

类型 特点
不可变 scala.collection.immutable 默认,线程安全
可变 scala.collection.mutable 需显式导入

下一步,让我们学习 List列表

最后更新:2026-03-27