集合操作 #
一、映射操作 #
1.1 map #
对每个元素应用函数:
scala
val list = List(1, 2, 3, 4, 5)
list.map(_ * 2)
list.map(x => x * x)
list.map(_.toString)
val map = Map("a" -> 1, "b" -> 2)
map.map { case (k, v) => (k.toUpperCase, v * 2) }
1.2 mapValues #
映射 Map 的值:
scala
val map = Map("a" -> 1, "b" -> 2, "c" -> 3)
map.mapValues(_ * 2)
1.3 flatMap #
映射后展平:
scala
val list = List(1, 2, 3)
list.flatMap(n => List(n, n * 2))
val words = List("Hello", "World")
words.flatMap(_.toList)
val nested = List(List(1, 2), List(3, 4), List(5, 6))
nested.flatMap(identity)
1.4 collect #
结合 filter 和 map:
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.collect {
case x if x % 2 == 0 => x * x
}
val mixed: List[Any] = List(1, "hello", 2, "world", 3.14)
mixed.collect { case i: Int => i }
二、过滤操作 #
2.1 filter #
保留满足条件的元素:
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.filter(_ > 5)
list.filter(_ % 2 == 0)
list.filter(_.toString.length > 1)
2.2 filterNot #
保留不满足条件的元素:
scala
val list = List(1, 2, 3, 4, 5)
list.filterNot(_ > 3)
2.3 takeWhile / dropWhile #
scala
val list = List(1, 2, 3, 4, 5, 1, 2, 3)
list.takeWhile(_ < 4)
list.dropWhile(_ < 4)
2.4 partition #
分成两组:
scala
val list = List(1, 2, 3, 4, 5, 6)
val (evens, odds) = list.partition(_ % 2 == 0)
2.5 span #
scala
val list = List(1, 2, 3, 4, 5, 1, 2)
val (first, rest) = list.span(_ < 4)
三、聚合操作 #
3.1 reduce #
scala
val list = List(1, 2, 3, 4, 5)
list.reduce(_ + _)
list.reduce(_ * _)
list.reduce(_ max _)
list.reduce(_ min _)
3.2 reduceLeft / reduceRight #
scala
val list = List(1, 2, 3, 4, 5)
list.reduceLeft(_ - _)
list.reduceRight(_ - _)
3.3 fold #
scala
val list = List(1, 2, 3, 4, 5)
list.fold(0)(_ + _)
list.fold(1)(_ * _)
list.fold("")(_ + _)
3.4 foldLeft / foldRight #
scala
val list = List(1, 2, 3, 4, 5)
list.foldLeft(0)(_ - _)
list.foldRight(0)(_ - _)
list.foldLeft(List.empty[Int])((acc, x) => x :: acc)
3.5 aggregate / combine #
scala
val list = List(1, 2, 3, 4, 5)
list.aggregate(0)(_ + _, _ + _)
四、分组操作 #
4.1 groupBy #
按条件分组:
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.groupBy(_ % 2)
list.groupBy(_ % 3)
case class Person(name: String, age: Int)
val people = List(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 25))
people.groupBy(_.age)
4.2 grouped #
按大小分组:
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.grouped(3).toList
list.grouped(4).toList
4.3 sliding #
滑动窗口:
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.sliding(3).toList
list.sliding(3, 2).toList
4.4 groupMapReduce #
scala
val list = List("apple", "banana", "cherry", "date", "elderberry")
list.groupMapReduce(_.head)(_.length)(_ + _)
五、排序操作 #
5.1 sorted #
scala
val list = List(3, 1, 4, 1, 5, 9, 2, 6)
list.sorted
list.sorted(Ordering[Int].reverse)
val strings = List("banana", "apple", "cherry")
strings.sorted
5.2 sortBy #
scala
case class Person(name: String, age: Int)
val people = List(
Person("Alice", 25),
Person("Bob", 30),
Person("Charlie", 20)
)
people.sortBy(_.age)
people.sortBy(_.name)
people.sortBy(p => (p.age, p.name))
5.3 sortWith #
scala
val list = List(3, 1, 4, 1, 5, 9, 2, 6)
list.sortWith(_ < _)
list.sortWith(_ > _)
case class Person(name: String, age: Int)
val people = List(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20))
people.sortWith((a, b) => a.age < b.age)
people.sortWith((a, b) => a.name < b.name)
六、查找操作 #
6.1 find #
scala
val list = List(1, 2, 3, 4, 5)
list.find(_ > 3)
list.find(_ > 10)
6.2 exists / forall #
scala
val list = List(1, 2, 3, 4, 5)
list.exists(_ > 3)
list.exists(_ > 10)
list.forall(_ > 0)
list.forall(_ > 3)
6.3 contains #
scala
val list = List(1, 2, 3, 4, 5)
list.contains(3)
list.contains(10)
6.4 indexOf / lastIndexOf #
scala
val list = List(1, 2, 3, 2, 1)
list.indexOf(2)
list.indexOf(2, 2)
list.lastIndexOf(2)
6.5 count #
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.count(_ > 5)
list.count(_ % 2 == 0)
七、组合操作 #
7.1 zip #
scala
val list1 = List(1, 2, 3)
val list2 = List("a", "b", "c")
list1.zip(list2)
val list3 = List(1, 2, 3, 4, 5)
val list4 = List("a", "b", "c")
list3.zip(list4)
7.2 zipAll #
scala
val list1 = List(1, 2, 3)
val list2 = List("a", "b")
list1.zipAll(list2, 0, "x")
list2.zipAll(list1, "x", 0)
7.3 zipWithIndex #
scala
val list = List("a", "b", "c", "d", "e")
list.zipWithIndex
list.zipWithIndex.map { case (value, index) => s"$index: $value" }
7.4 unzip #
scala
val pairs = List((1, "a"), (2, "b"), (3, "c"))
val (numbers, letters) = pairs.unzip
八、其他操作 #
8.1 distinct #
scala
val list = List(1, 1, 2, 2, 3, 3, 4, 4, 5)
list.distinct
8.2 diff / intersect / union #
scala
val list1 = List(1, 2, 3, 4, 5)
val list2 = List(3, 4, 5, 6, 7)
list1.diff(list2)
list1.intersect(list2)
list1.union(list2)
8.3 slice #
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
list.slice(2, 5)
list.slice(0, 3)
8.4 splitAt #
scala
val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val (first, second) = list.splitAt(5)
8.5 mkString #
scala
val list = List(1, 2, 3, 4, 5)
list.mkString
list.mkString(", ")
list.mkString("[", ", ", "]")
九、实战示例 #
9.1 数据处理管道 #
scala
case class Sale(product: String, amount: Double, date: String)
val sales = List(
Sale("A", 100, "2024-01"),
Sale("B", 200, "2024-01"),
Sale("A", 150, "2024-02"),
Sale("B", 250, "2024-02")
)
val byProduct = sales
.groupBy(_.product)
.map { case (product, sales) =>
product -> sales.map(_.amount).sum
}
val byMonth = sales
.groupBy(_.date)
.map { case (month, sales) =>
month -> sales.map(_.amount).sum
}
9.2 词频统计 #
scala
def wordCount(text: String): Map[String, Int] =
text.toLowerCase
.split("\\W+")
.filter(_.nonEmpty)
.groupBy(identity)
.map { case (word, occurrences) => word -> occurrences.length }
val text = "Hello world hello Scala world Scala scala"
println(wordCount(text))
9.3 数据验证 #
scala
case class User(name: String, age: Int, email: String)
val users = List(
User("Alice", 25, "alice@example.com"),
User("Bob", -5, "bob@example.com"),
User("Charlie", 30, "invalid-email"),
User("David", 200, "david@example.com")
)
val validUsers = users.filter { user =>
user.age > 0 && user.age < 150 && user.email.contains("@")
}
val errors = users.map { user =>
val errors = List(
if user.age <= 0 then Some("Age must be positive") else None,
if user.age >= 150 then Some("Age must be less than 150") else None,
if !user.email.contains("@") then Some("Invalid email") else None
).flatten
user.name -> errors
}.filter(_._2.nonEmpty)
十、总结 #
操作分类 #
| 类型 | 方法 |
|---|---|
| 映射 | map, flatMap, collect |
| 过滤 | filter, filterNot, takeWhile |
| 聚合 | reduce, fold, sum, product |
| 分组 | groupBy, grouped, sliding |
| 排序 | sorted, sortBy, sortWith |
| 查找 | find, exists, forall, contains |
| 组合 | zip, unzip, zipWithIndex |
性能建议 #
- 使用 view 延迟计算
- 避免多次遍历
- 选择合适的集合类型
- 使用 collect 替代 filter + map
下一步,让我们学习 泛型!
最后更新:2026-03-27