Scala实现wordCount
作者:
lehuai | 来源:发表于
2018-01-03 10:01 被阅读0次package day02
/**
* Scala实现WordCount
*/
object ScalaWordCount {
def main(args: Array[String]): Unit = {
val lines = List("hello java hello python","hello scala","hello scala hello java hello scala")
// 切分并压平
val words = lines.flatMap(_.split(" "))
// 把每个单词生成一个一个的pair
val tuples = words.map((_,1))
// 以key(单词)进行分组
val grouped = tuples.groupBy(_._1)
// 统计value长度
val sumed = grouped.mapValues(_.size)
// 排序
val sorted = sumed.toList.sortBy(_._2)
// 降序排列
val result = sorted.reverse
println(result)
}
}
本文标题:Scala实现wordCount
本文链接:https://www.haomeiwen.com/subject/cqthnxtx.html
网友评论