wordcount
目录下有两个文件:word1.txt
、word2.txt
程序的目的就是统计这两个文件中单词的数量
其中word1.txt
中内容为:
Hello World Hello Scala
Scala Learning
For Spark
Scala
word2.txt
中内容为:
Hello Java
Python Scala
For Spark
Spark Learning
Scala代码如下:(输出结果见后)
给出两种代码,foreach
与 for ()
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