![第 7 章 集合-----Scala集合继承图_第1张图片](http://img.e-com-net.com/image/info8/179ae5c88f0240e5bb119aa7fecd3be5.jpg)
7.1.1 不可变集合继承图
![第 7 章 集合-----Scala集合继承图_第2张图片](http://img.e-com-net.com/image/info8/6082f5ac4a6548a489b13751e07bd5c5.jpg)
![第 7 章 集合-----Scala集合继承图_第3张图片](http://img.e-com-net.com/image/info8/dda6b53a0e404b05a1a5f9eb8cf4be2f.jpg)
![第 7 章 集合-----Scala集合继承图_第4张图片](http://img.e-com-net.com/image/info8/ad5f93c2ce6640f9a7ba2ca6eb947fc2.jpg)
7.1.2 可变集合继承图
7.2.3 不可变数组与可变数组的转换
![第 7 章 集合-----Scala集合继承图_第5张图片](http://img.e-com-net.com/image/info8/a7526f79dc7447c7a41279607c74de38.jpg)
7.7.6 复杂 WordCount 案例
1)方式一
object TestWordCount {
def main(args: Array[String]): Unit = {
val tupleList = List(("Hello Scala Spark World ", 4), ("Hello
Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))
val stringList: List[String] = tupleList.map(t=>(t._1 + "
") * t._2)
stringList.flatMap(s=>s.split(" "))
val words: List[String] = stringList.flatMap(_.split(" "))
val groupMap: Map[String, List[String]] =
words.groupBy(word=>word)
words.groupBy(_)
val wordToCount: Map[String, Int] = groupMap.map(t=>(t._1,
t._2.size))
val wordCountList: List[(String, Int)] =
wordToCount.toList.sortWith {
(left, right) => {
left._2 > right._2
}
}.take(3)
")).groupBy(word=>word).map(t=>(t._1, t._2.size))
println(wordCountList)
}
}
2)方式二
object TestWordCount {
def main(args: Array[String]): Unit = {
val tuples = List(("Hello Scala Spark World", 4), ("Hello
Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))
val wordToCountList: List[(String, Int)] = tuples.flatMap
{
t => {
val strings: Array[String] = t._1.split(" ")
strings.map(word => (word, t._2))
}
}
val wordToTupleMap: Map[String, List[(String, Int)]] =
wordToCountList.groupBy(t=>t._1)
val stringToInts: Map[String, List[Int]] =
wordToTupleMap.mapValues {
datas => datas.map(t => t._2)
}
stringToInts
val wordToCountMap: Map[String, List[Int]] =
wordToTupleMap.map {
t => {
(t._1, t._2.map(t1 => t1._2))
}
}
val wordToTotalCountMap: Map[String, Int] =
wordToCountMap.map(t=>(t._1, t._2.sum))
println(wordToTotalCountMap)
}
}