集合计算简单函数
1、说明
(1)求和
(2)求乘积
(3)最大值
(4)最小值
(5)排序
2、案例实操
object demo29{
def main(args: Array[String]): Unit = {
val numList: List[Int] = List(2, -8, -1, 5, 3, -9, 4)
// 求和
println("列表元素之和: " + numList.sum)
// 求乘积
println("列表元素之积: " + numList.product)
// 最大值
println("列表中的最大值: " + numList.max)
// 最小值
println("列表中的最小值: " + numList.min)
// 按照元素大小排序
println("按元素大小排序后的列表: " + numList.sortBy(x => x))
// 按照元素的绝对值大小排序
println("按元素绝对值大小排序后的列表: " + numList.sortBy(x => x.abs))
}
}
集合计算高级函数
1、说明
(1)过滤
遍历一个集合并从中获取满足指定条件的元素组成一个新的集合
(2)转化/映射(map)
将集合中的每一个元素映射到某一个函数
(3)扁平化
(4)扁平化+映射 注:flatMap 相当于先进行 map 操作,在进行 flatten 操作集合中的每个元素的子元素映射到某个函数并返回新集合
(5)分组(group)
按照指定的规则对集合的元素进行分组
(6)简化(归约)
(7)折叠
2、案例实操
object demo30 {
def main(args: Array[String]): Unit = {
// 定义整数列表
val intList: List[Int] = List(3, 4, 5, 6, 7, 8, 9)
// 定义嵌套列表
val nestedIntList: List[List[Int]] = List(List(10, 11, 12), List(13, 14, 15), List(16, 17, 18))
// 定义字符串列表
val stringList: List[String] = List("apple-banana", "cherry-date", "elderberry-fig")
// (1) 过滤:筛选出偶数
println("过滤后的结果(偶数): " + intList.filter(x => x % 2 == 0))
// (2) 转换/映射:将列表中的每个整数加2
println("映射后的结果(每个数加2): " + intList.map(x => x + 2))
// (3) 扁平化:将嵌套列表展平
println("扁平化后的结果: " + nestedIntList.flatten)
// (4) 扁平化+映射:先将字符串按 - 分割,再展平
println("扁平化+映射后的结果: " + stringList.flatMap(x => x.split("-")))
// (5) 分组:按照数字除以3的余数分组
println("分组后的结果: " + intList.groupBy(x => x % 3))
}
}
3、Reduce 方法
Reduce 简化(归约) :通过指定的逻辑将集合中的数据进行聚合,从而减少数据,最终获取结果。
案例实操
object demo31 {
def main(args: Array[String]): Unit = {
val numList = List(5, 3, 2, 4)
// 使用reduce实现乘法运算
val product = numList.reduce((x, y) => x * y)
println("列表元素乘积: " + product)
// 使用reduceLeft实现从左到右累加
val sumLeft = numList.reduceLeft((x, y) => x + y)
println("从左到右累加结果: " + sumLeft)
// 使用reduceRight实现从右到左相减
val subtractRight = numList.reduceRight((x, y) => x - y)
println("从右到左相减结果: " + subtractRight)
}
}
4、Fold 方法
Fold 折叠:化简的一种特殊情况
object demo32 {
def main(args: Array[String]): Unit = {
val numbers = List(5, 3, 2, 4)
// 使用foldLeft计算列表元素之和,初始值为0
val sum = numbers.foldLeft(0)((acc, num) => acc + num)
println("列表元素之和: " + sum)
// 使用foldRight计算从右到左的乘积,初始值为1
val productRight = numbers.foldRight(1)((num, acc) => num * acc)
println("从右到左的乘积: " + productRight)
// 使用foldLeft计算累减,初始值为10
val subtractLeft = numbers.foldLeft(10)((acc, num) => acc - num)
println("累减结果: " + subtractLeft)
}
}
7.2.1普通WordCount 案例
1、需求
单词计数:将集合中出现的相同的单词,进行计数,取计数排名前三的结果
3、案例实操
object demo33 {
def main(args: Array[String]): Unit = {
// 定义包含文本的字符串列表
val textList = List("apple banana apple", "banana cherry", "cherry apple")
// 1. 将每一个字符串转换成一个个单词
val wordList = textList.flatMap(str => str.split(" "))
// println(wordList)
// 2. 将相同的单词放置在一起
val wordToWordsMap: Map[String, List[String]] = wordList.groupBy(word => word)
// println(wordToWordsMap)
// 3. 对相同的单词进行计数
val wordToCountMap: Map[String, Int] = wordToWordsMap.map(tuple => (tuple._1, tuple._2.size))
// println(wordToCountMap)
// 4. 对计数完成后的结果进行排序(降序)
val sortedWordCountList = wordToCountMap.toList.sortBy(_._2)(Ordering[Int].reverse)
// println(sortedWordCountList)
// 5. 取计数排名前三的结果
val topThreeWords = sortedWordCountList.take(3)
println("计数排名前三的单词:")
topThreeWords.foreach { case (word, count) => println(s"单词: $word,计数: $count") }
}
}
复杂WordCount 案例
案例实操
object demo34{
def main(args: Array[String]): Unit = {
// 定义包含文本及对应频次的元组列表
val tupleList = List(("apple banana apple", 3), ("banana cherry", 2), ("cherry apple", 2))
// 将元组列表转换为字符串列表,重复对应频次的次数
val stringList = tupleList.flatMap { case (text, count) => List.fill(count)(text) }
// 拆分字符串为单词列表
val words: List[String] = string
List.flatMap(_.split(" "))
// 将相同单词分组
val groupMap: Map[String, List[String]] = words.groupBy(word => word)
// 统计每个单词的数量
val wordToCount: Map[String, Int] = groupMap.map { case (word, wordList) => (word, wordList.size) }
// 将单词计数结果转换为列表并按数量降序排序
val sortedWordCountList: List[(String, Int)] = wordToCount.toList.sortBy(_._2)(Ordering[Int].reverse)
// 打印结果
sortedWordCountList.foreach { case (word, count) => println(s"单词: $word,计数: $count") }
}
}
并行集合
1、说明
Scala 为了充分使用多核 CPU,提供了并行集合(有别于前面的串行集合),用于多核环境的并行计算。
2、案例实操
object demo35 {
def main(args: Array[String]): Unit = {
// 创建一个包含1到1000的整数列表,并转换为并行集合
val numbers = (1 to 1000).toList.par
// 使用并行集合的map方法计算每个数字的平方
val squaredNumbers = numbers.map(x => x * x)
// 打印结果(这里只打印前几个元素示意)
squaredNumbers.take(10).foreach(println)
}
}