hello word hello word you and me
you and me hello word cat dog cat
dog and me you and his miss
在idea中创建一个scala_project,并创建一个WordCountApp的object
import scala.io.Source
object WordCountApp {
def main(args: Array[String]): Unit = {
//the path of the file
val filepath = "/Users/liujingmao/Downloads/scala_wordcount"
/**
* hello word hello word you and me
* you and me hello word cat dog cat
* dog and me you and his miss
*/
// set utf-8
val codec="utf-8"
// read the file from specific file path with passing two parameters(filepath,codec)
val file=Source.fromFile(filepath,codec)
// get the line
val line=file.getLines()
//.foreach(println)的结果如下所示:
/**
* hello word hello word you and me
* you and me hello word cat dog cat
* dog and me you and his miss
*/
.flatMap(_.split(" ")).toList
//.foreach(println)的结果如下所示:
/**
* hello
* word
* hello
* word
* ......
*/
.map((_,1))
//.foreach(println)的结果如下所示
/**
* (hello,1)
* (word,1)
* (hello,1)
* (word,1)
* .......
*/
.groupBy(_._1)
//.foreach(println)的结果如下所示
/**
* (his,List((his,1)))
* (dog,List((dog,1), (dog,1)))
* (miss,List((miss,1)))
* (you,List((you,1), (you,1), (you,1)))
* .......
*/
.mapValues(_.size).foreach(println)
//.foreach(println)的结果如下所示:
/**
* (his,1)
* (dog,2)
* (miss,1)
* (you,3)
* (me,3)
* (hello,3)
* (cat,2)
* (word,3)
* (and,4)
*/
//close the file
file.close( )
}
}