object WordCountByScala {
val arr = Array("hello tom and cat", "hello boy and girl")
def main(args: Array[String]): Unit = {
val r1: Array[Array[String]] = arr.map(_.split(" "))
val r2: Array[String] = r1.flatten
val r3: Map[String, Array[String]] = r2.groupBy(x => x)
val r4: Map[String, Int] = r3.map(kv => (kv._1, kv._2.length))
val r5: List[(String, Int)] = r4.toList.sortBy(-_._2)
println(r5.toBuffer)
val r6: List[(String, Int)] = arr.flatMap(_.split(" ")).groupBy(x => x).mapValues(_.length).toList.sortBy(-_._2)
println(r6.toBuffer)
}
}
Scala之 WordCount
最新推荐文章于 2023-11-20 15:31:37 发布