scala 实现wordcount

object ScalaWordCount {
  def main(args: Array[String]): Unit = {
    var lines = List("hello java hello scala", "hello tom", "today is good day hello scala", "day by day")

    //切分并压平
    val words = lines.flatMap(_.split(" "))

    // 把每个单词生成一个一个pair(key, 1)
    val tuples = words.map((_, 1))

    //以key进行分组 第一个_代表元组,第二个_1 代表key(单词)
    val grouped = tuples.groupBy(_._1)

    //统计value的长度
    val sumed = grouped.mapValues(_.size)

    //排序
    val sorted = sumed.toList.sortBy(_._2).reverse
    println(sorted)
  }
}

猜你喜欢

转载自blog.csdn.net/zhc794429974/article/details/81809356