统计一个文件中单词个数, 最传统的写法
package day3 import scala.collection.mutable import scala.io.Source object demo_wordcount { def main(args: Array[String]): Unit = { // 读取文件内容 val linesInterator = Source.fromFile("D:\scalas\wordc.txt").getLines() // 统计单词出现次数, 定义可变map val map = mutable.Map[String, Int]() for(line <- linesInterator){ // 分割字符串 val words = line.split("\s+") for(word <- words){ // 开始时map中是空值 val cntOption = map.get(word) if(cntOption.isDefined){ val cnt = cntOption.get +1 map.put(word, cnt) }else map.put(word,1) } } // 打印生成的map for((k, v) <- map){ println(s"${k}-->${v}") } } }
试试函数式编程来解决
package day3 import scala.io.Source object demo_wordcount2 { def main(args: Array[String]): Unit = { // 读取文件内容 val linesInterator = Source.fromFile("D:\scalas\wordc.txt").getLines() // 切分数据 // linesInterator.flatMap(line =>line.split("\s+")) val words = linesInterator.flatMap(_.split("\s+")) //分组 // val groupwords = words.toArray.groupBy(word => word).foreach(t=>println(t._1 + "--->"+t._2.mkString(","))) val groupwords = words.toArray.groupBy(word => word) // 聚合 单词统计 val wordcounts = groupwords.map(kv => (kv._1,kv._2.length)) println(wordcounts) } }
打印同样的结果 :
Map(meimei -> 1, me -> 1, he -> 1, hello -> 4, ligang -> 1)
能不能再简单点呢, 好吧, 一句话搞定
package day3 import scala.io.Source object demo_wordcount2 { def main(args: Array[String]): Unit = { Source.fromFile("D:\scalas\wordc.txt").getLines() .flatMap(_.split("\s+")).toArray .groupBy(word => word) .map(kv => (kv._1,kv._2.length)) .foreach(println) } }