Scala小案例:WordCount

wordcount目录下有两个文件:word1.txtword2.txt
程序的目的就是统计这两个文件中单词的数量
其中word1.txt中内容为:

Hello World Hello Scala
Scala Learning
For Spark
Scala

word2.txt中内容为:

Hello Java
Python Scala
For Spark
Spark Learning

Scala代码如下:(输出结果见后)
给出两种代码,foreachfor ()

import java.io.File
import scala.io.Source

object WordCount {

  def main(args: Array[String]): Unit = {
    val dirFile = new File("D:\\_Zsp_Space\\Scala\\Scala_Learning\\src\\main\\wordcount")
    val files = dirFile.listFiles
    for (file <- files) println(file);println()
    val listFiles = files.toList
    val wordsMap = scala.collection.mutable.Map[String, Int]()
/*
    listFiles.foreach(file =>Source.fromFile(file).getLines().foreach(line =>line.split(" ").
      foreach(
        word => {
          if (wordsMap.contains(word)) {
            wordsMap(word) += 1
          } else {
            wordsMap +=(word->1)
          }
        }
      )
    )
    )
*/
    for (file <- listFiles) {
      println(file)
      val demo_file = Source.fromFile(file).getLines()
      for (line <- demo_file) {
        println("the line is: "+line)
        val demo_line = line.split(" ")
        for (word <- demo_line) {
          println(word)
          if (wordsMap.contains(word)) {
            wordsMap(word) += 1
          } else {
            wordsMap += (word -> 1)
          }
        }
      }
    }

    println(wordsMap)
    for ((key, value) <- wordsMap) println(key + ": " + value)

  }

}

输出结果:

D:\_Zsp_Space\Scala\Scala_Learning\src\main\wordcount\word1.txt
D:\_Zsp_Space\Scala\Scala_Learning\src\main\wordcount\word2.txt

D:\_Zsp_Space\Scala\Scala_Learning\src\main\wordcount\word1.txt
the line is: Hello World Hello Scala
Hello
World
Hello
Scala
the line is: Scala Learning
Scala
Learning
the line is: For Spark
For
Spark
the line is: Scala
Scala
D:\_Zsp_Space\Scala\Scala_Learning\src\main\wordcount\word2.txt
the line is: Hello Java
Hello
Java
the line is: Python Scala
Python
Scala
the line is: For Spark
For
Spark
the line is: Spark Learning
Spark
Learning
Map(For -> 2, Hello -> 3, Learning -> 2, Spark -> 3, Scala -> 4, World -> 1, Java -> 1, Python -> 1)
For: 2
Hello: 3
Learning: 2
Spark: 3
Scala: 4
World: 1
Java: 1
Python: 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值