您的位置:首页 > 其它

scala实现wordcount

2016-09-06 13:12 417 查看
这里给出两种方式,供初学者参考。

方式一:

val lines = List("show me the money", "show me the meaning of being lonely", "triple kill", "mega kill","monster kill","holy shit")

val map = lines.flatMap { line => line.split(" ") }.foldLeft(Map.empty[String, Int]){(count, word) => count + (word -> (count.getOrElse(word, 0) + 1))}


方式二:

lines.flatMap { line => line.split(" ") }.map { x => (x,1) }.groupBy(_._1).mapValues(f=>f.size).foreach(println)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scala