开发者社区 问答 正文

用 scala 写一个 wordcount ?

用 scala 写一个 wordcount ?

展开
收起
愚笨如你 2020-02-14 20:06:28 833 分享 版权
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
    赞同 展开评论
问答分类:
问答标签:
问答地址: