我听说WordCount用scala不是只需要两行吗,为什么我…
import java.io.FileWriter
import scala.collection.mutable
object MyWCApp extends App{
val contents=
"""hello,world,hello,world,count,world
|xixixi,hehehe,hahaha,world,count,count
|hello""".stripMargin
//val contents =scala.io.Source.fromFile("input/hell.txt").mkString
//println(contents)
val tmp = mutable.ListBuffer[(String,Int)]()
for(line<-contents.split("\r\n")){
tmp++=List(line.trim).flatMap(_.split(",")).map(x=>(x,1))
}
val mp = mutable.Map[String,Int]()
for((k,v)<-tmp){
if (mp.getOrElse(k, -1) != -1) {
mp(k) = v + mp(k)
} else {
mp(k) = 1
}
}
val b= new FileWriter("output/ans.txt")
for ((k,v)<-mp){
b.write(s"$k\t$v")
b.write(10)
}
b.close()
}