wordCount
package com.doit.day03
import scala.io.{BufferedSource, Source}
object WordCountDemo {
def main(args: Array[String]): Unit = {
val source: BufferedSource = Source.fromFile("D:\\develop\\ideaWorkSpace\\myself\\study\\scalaDemo\\data\\word.txt")
val lines: Iterator[String] = source.getLines()
val arrWord: Iterator[Array[String]] = lines.map(line => {
val wordsArr: Array[String] = line.split("\\s+")
wordsArr
})
val list: List[Array[String]] = arrWord.toList
val word: List[String] = list.flatten
val wordAndList: Map[String, List[String]] = word.groupBy(word => word)
val wordCount: Map[String, Int] = wordAndList.map(tup => (tup._1, tup._2.size))
println(wordCount)
}
}