使用foldLeft函数,实现简易的wordCount
import scala.collection.mutable object Demo_019 { def main(args: Array[String]): Unit = { val list = List("bigdata han hello ", "bigdata han aaa aaa aaa ccc ddd uuu") val map01 = mutable.Map[String, Int]() list.foldLeft(map01)(count2) println(map01) } def count2(map2: mutable.Map[String, Int], str: String): mutable.Map[String, Int] = { val arr: Array[String] = str.split(" ") for (elem <- arr) { map2 += (elem -> (map2.getOrElse(elem, 0) + 1)) } map2 }
运行结果为:
当然这种方式,还是显得太罗嗦了,还有更为简洁的方式,而且还可以结果进行正序或逆序排序
简单一句就是:
val result = list.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(s => (s._1,s._2.size))
显然不容易懂,下面是详细说明
object Demo_019_01 { def main(args: Array[String]): Unit = { val list = List("bigdata han hello ", "bigdata han aaa aaa aaa ccc ddd uuu") // val result2 = list.flatMap((x: String) => x.split(" ")) val result2 = list.flatMap(_.split(" ")) println("result2:"+result2) // val result3 = result2.map((x: String) => (x, 1)) val result3 = result2.map((_,1)) println("result3:"+result3) // val result4 = result3.groupBy((x: (String, Int)) => x._1) val result4 = result3.groupBy(_._1) println("result4:"+result4) // val result5 = result4.map((s: (String, List[(String, Int)])) => (s._1, s._2.size)) val result5= result4.map(s => (s._1,s._2.size)) println("result5:"+result5) // val result6 = result5.toList.sortBy((x: (String, Int)) => x._2).reverse val result6 = result5.toList.sortBy(_._2) println("result6:"+result6) } }
输出结果为
上面使用了参数类型推断,关于参数类型推断,介绍如下