scala高階函數之map


 

統計一個文件中單詞個數, 最傳統的寫法

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)

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM