第六课 尚硅谷Scala语言学习-集合常用函数
第一节 基本属性和常用操作
- 获取集合长度
- 获取集合大小
- 循环遍历
- 迭代器
- 生成字符串
- 是否包含
package chapter07
object Test11_CommonOp {
def main(args: Array[String]): Unit = {
val list = List(1,3,5,7,2,89)
val set = Set(23,34,423,75)
println(list.length)
println(set.size)
for (elem <- list)
println(elem)
set.foreach(println)
for (elem <- list.iterator) println(elem)
println("====================")
println(list)
println(set)
println(list.mkString("--"))
println(list.contains(23))
println(set.contains(23))
}
}
第二节 衍生集合
- 获取集合的头
- 获取集合的尾(不是头的就是尾)
- 集合最后一个数据
- 集合初始数据(不包含最后一个)
- 反转
- 取前(后)n 个元素
- 去掉前(后)n 个元素
- 并集
- 交集
- 差集
- 拉链
- 滑窗
package chapter07
object Test12_DerivedCollection {
def main(args: Array[String]): Unit = {
val list1 = List(1,3,5,7,2,89)
val list2 = List(3,7,2,45,4,8,19)
println(list1.head)
println(list1.tail)
println(list2.last)
println(list2.init)
println(list1.reverse)
println(list1.take(3))
println(list1.takeRight(4))
println(list1.drop(3))
println(list1.dropRight(4))
println("=========================")
val union = list1.union(list2)
println("union: " + union)
println(list1 ::: list2)
val set1 = Set(1,3,5,7,2,89)
val set2 = Set(3,7,2,45,4,8,19)
val union2 = set1.union(set2)
println("union2: " + union2)
println(set1 ++ set2)
println("-----------------------")
val intersection = list1.intersect(list2)
println("intersection: " + intersection)
println("-----------------------")
val diff1 = list1.diff(list2)
val diff2 = list2.diff(list1)
println("diff1: " + diff1)
println("diff2: " + diff2)
println("-----------------------")
println("zip: " + list1.zip(list2))
println("zip: " + list2.zip(list1))
println("-----------------------")
for (elem <- list1.sliding(3))
println(elem)
println("-----------------------")
for (elem <- list2.sliding(4, 2))
println(elem)
println("-----------------------")
for (elem <- list2.sliding(3, 3))
println(elem)
}
}
第三节 集合计算简单函数
- 求和
- 求乘积
- 最大值
- 最小值
- 排序
package chapter07
object Test13_SimpleFunction {
def main(args: Array[String]): Unit = {
val list = List(5,1,8,2,-3,4)
val list2 = List(("a", 5), ("b", 1), ("c", 8), ("d", 2), ("e", -3), ("f", 4))
var sum = 0
for (elem <- list){
sum += elem
}
println(sum)
println(list.sum)
println(list.product)
println(list.max)
println(list2.maxBy( (tuple: (String, Int)) => tuple._2 ))
println(list2.maxBy( _._2 ))
println(list.min)
println(list2.minBy(_._2))
println("========================")
val sortedList = list.sorted
println(sortedList)
println(list.sorted.reverse)
println(list.sorted(Ordering[Int].reverse))
println(list2.sorted)
println(list2.sortBy(_._2))
println(list2.sortBy(_._2)(Ordering[Int].reverse))
println(list.sortWith( (a: Int, b: Int) => {a < b} ))
println(list.sortWith( _ < _ ))
println(list.sortWith( _ > _))
}
}
第四节 集合计算高级函数
- 过滤:遍历一个集合并从中获取满足指定条件的元素组成一个新的集合
- 转化/映射(map):将集合中的每一个元素映射到某一个函数
- 扁平化+映射 注:flatMap 相当于先进行 map 操作,在进行 flatten 操作集合中的每个元素的子元素映射到某个函数并返回新集合
- 分组(group):按照指定的规则对集合的元素进行分组
- 简化(归约)
- 折叠
- Map类计算函数案例如下
package chapter07
object Test14_HighLevelFunction_Map {
def main(args: Array[String]): Unit = {
val list = List(1,2,3,4,5,6,7,8,9)
val evenList = list.filter( (elem: Int) => {elem % 2 == 0} )
println(evenList)
println(list.filter( _ % 2 == 1 ))
println("=======================")
println(list.map(_ * 2))
println(list.map( x => x * x))
println("=======================")
val nestedList: List[List[Int]] = List(List(1,2,3),List(4,5),List(6,7,8,9))
val flatList = nestedList(0) ::: nestedList(1) ::: nestedList(2)
println(flatList)
val flatList2 = nestedList.flatten
println(flatList2)
println("=======================")
val strings: List[String] = List("hello world", "hello scala", "hello java", "we study")
val splitList: List[Array[String]] = strings.map( _.split(" ") )
val flattenList = splitList.flatten
println(flattenList)
val flatmapList = strings.flatMap(_.split(" "))
println(flatmapList)
println("========================")
val groupMap: Map[Int, List[Int]] = list.groupBy( _ % 2)
val groupMap2: Map[String, List[Int]] = list.groupBy( data => if (data % 2 == 0) "偶数" else "奇数")
println(groupMap)
println(groupMap2)
val wordList = List("china", "america", "alice", "canada", "cary", "bob", "japan")
println( wordList.groupBy( _.charAt(0) ) )
}
}
- Reduce 简化(归约) :通过指定的逻辑将集合中的数据进行聚合,从而减少数据,最终获取结果。
package chapter07
object Test15_HighLevelFunction_Reduce {
def main(args: Array[String]): Unit = {
val list = List(1,2,3,4)
println(list.reduce( _ + _ ))
println(list.reduceLeft(_ + _))
println(list.reduceRight(_ + _))
println("===========================")
val list2 = List(3,4,5,8,10)
println(list2.reduce(_ - _))
println(list2.reduceLeft(_ - _))
println(list2.reduceRight(_ - _))
println("===========================")
println(list.fold(10)(_ + _))
println(list.foldLeft(10)(_ - _))
println(list2.foldRight(11)(_ - _))
}
}
- 实例操作。两个 Map 的数据合并
package chapter07
import scala.collection.mutable
object Test16_MergeMap {
def main(args: Array[String]): Unit = {
val map1 = Map("a" -> 1, "b" -> 3, "c" -> 6)
val map2 = mutable.Map("a" -> 6, "b" -> 2, "c" -> 9, "d" -> 3)
val map3 = map1.foldLeft(map2)(
(mergedMap, kv) => {
val key = kv._1
val value = kv._2
mergedMap(key) = mergedMap.getOrElse(key, 0) + value
mergedMap
}
)
println(map3)
}
}
第五节 普通 WordCount 案例
- 单词计数:将集合中出现的相同的单词,进行计数,取计数排名前三的结果
package chapter07
object Test17_CommonWordCount {
def main(args: Array[String]): Unit = {
val stringList: List[String] = List(
"hello",
"hello world",
"hello scala",
"hello spark from scala",
"hello flink from scala"
)
val wordList = stringList.flatMap(_.split(" "))
println(wordList)
val groupMap: Map[String, List[String]] = wordList.groupBy(word => word)
println(groupMap)
val countMap: Map[String, Int] = groupMap.map(kv => (kv._1, kv._2.length))
val sortList: List[(String, Int)] = countMap.toList
.sortWith( _._2 > _._2 )
.take(3)
println(sortList)
}
}
第六节 复杂 WordCount 案例
- 这里数据和之前不同。
package chapter07
object Test18_ComplexWordCount {
def main(args: Array[String]): Unit = {
val tupleList: List[(String, Int)] = List(
("hello", 1),
("hello world", 2),
("hello scala", 3),
("hello spark from scala", 1),
("hello flink from scala", 2)
)
val newStringList: List[String] = tupleList.map(
kv => {
(kv._1.trim + " ") * kv._2
}
)
println(newStringList)
val wordCountList: List[(String, Int)] = newStringList
.flatMap(_.split(" "))
.groupBy( word => word )
.map( kv => (kv._1, kv._2.size) )
.toList
.sortBy(_._2)(Ordering[Int].reverse)
.take(3)
println(wordCountList)
println("================================")
val preCountList: List[(String, Int)] = tupleList.flatMap(
tuple => {
val strings: Array[String] = tuple._1.split(" ")
strings.map( word => (word, tuple._2) )
}
)
println(preCountList)
val preCountMap: Map[String, List[(String, Int)]] = preCountList.groupBy( _._1 )
println(preCountMap)
val countMap: Map[String, Int] = preCountMap.mapValues(
tupleList => tupleList.map(_._2).sum
)
println(countMap)
val countList = countMap.toList
.sortWith(_._2 > _._2)
.take(3)
println(countList)
}
}
第七节 队列
- Scala 也提供了队列(Queue)的数据结构,队列的特点就是先进先出。进队和出队的方法分别为 enqueue 和 dequeue。
package chapter07
import scala.collection.immutable.Queue
import scala.collection.mutable
import scala.collection.parallel.immutable
object Test19_Queue {
def main(args: Array[String]): Unit = {
val queue: mutable.Queue[String] = new mutable.Queue[String]()
queue.enqueue("a", "b", "c")
println(queue)
println(queue.dequeue())
println(queue)
println(queue.dequeue())
println(queue)
queue.enqueue("d", "e")
println(queue)
println(queue.dequeue())
println(queue)
println("==========================")
val queue2: Queue[String] = Queue("a", "b", "c")
val queue3 = queue2.enqueue("d")
println(queue2)
println(queue3)
}
}
第八节 并行集合
- Scala 为了充分使用多核 CPU,提供了并行集合(有别于前面的串行集合),用于多核环境的并行计算。
package chapter07
import scala.collection.immutable
import scala.collection.parallel.immutable.ParSeq
object Test20_Parallel {
def main(args: Array[String]): Unit = {
val result: immutable.IndexedSeq[Long] = (1 to 100).map(
x => Thread.currentThread.getId
)
println(result)
val result2: ParSeq[Long] = (1 to 100).par.map(
x => Thread.currentThread.getId
)
println(result2)
}
}