开发者社区> 问答> 正文

用 scala 写一个 wordcount ?

用 scala 写一个 wordcount ?

展开
收起
愚笨如你 2020-02-14 20:06:28 792 0
1 条回答
写回答
取消 提交回答
  • 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)) // k,v分组 val grouped = tuples.groupBy(.1) val sumed = grouped.mapValues(.size) // 排序 val sorted = sumed.toList.sortBy(._2) // 降序 val result = sorted.reverse println(result) } }

    2020-02-14 20:07:42
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JDK8新特性与生产-for“华东地区scala爱好者聚会” 立即下载
Just Enough Scala for Spark 立即下载
From Python Scikit learn to Scala Spark 立即下载