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)