val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")spa
第一種:
lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))scala
第二種:
lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t=>(t._1, t._2.size)).toList.sortBy(_._2).reverseit
分步說明:io
1:lines.flatMap(_.split(" "))table
------將lines壓平處理成單個單詞------List
結果: res16: List[String] = List(hello, tom, hello, jerry, hello, jerry, hello, kitty)map
2:lines.flatMap(_.split(" ")).map((_, 1))im
------分解成元組(word,1) -----sort
結果:res17: List[(String, Int)] = List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (jerry,1), (hello,1), (kitty,1))word
3:lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1)
------按單詞分組 -----
結果:res18: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(tom -> List((tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1), (jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1)))
4:lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
------取出map的value值,疊加。。
mapValues(_.foldLeft(0)(_+_._2)) 說明:第一個下劃線是List((jerry,1), (jerry,1)) -----
第二個下劃線是foldLeft累加後的結果-----
第三個下劃線是 (jerry,1) -----
結果: res19: scala.collection.immutable.Map[String,Int] = Map(tom -> 1, kitty -> 1, jerry -> 2, hello -> 4)