zoukankan      html  css  js  c++  java
  • 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)
    
        }
    
    }
  • 相关阅读:
    Ubuntu包管理命令 dpkg、apt和aptitude
    Linux curses库使用
    VC皮肤库SkinSharp 1.0.6.6的使用
    HOG(方向梯度直方图)
    2014年国外发布的中国内地大学排名18强名单
    sql语句中BEGIN TRAN...COMMIT TRAN
    搜索框中“请输入搜索keyword”
    IOS基于新浪微博开放平台微博APP
    php字符串标点等字符截取不乱吗 封装方法
    谈一谈struts2和springmvc的拦截器
  • 原文地址:https://www.cnblogs.com/kongzhagen/p/15178072.html
Copyright © 2011-2022 走看看