package day0811
import scala.annotation.varargs
object liujingwordcount {
def main(args: Array[String]): Unit = {
val arr=Array("hello word ketty","hello jeryy","hello tomcat","tomcat good")
//[hello word ketty, hello jeryy, hello tomcat, tomcat good]
val arr0=arr.flatMap { x => x.split(" ") }
//[(hello,1), (word,1), (ketty,1), (hello,1), (jeryy,1), (hello,1), (tomcat,1), (tomcat,1), (good,1)]
val arr1=arr0.map { x => (x,1) }
// Map(good -> [Lscala.Tuple2;@1d318953, jeryy -> [Lscala.Tuple2;@1f2eb71e, ketty -> [Lscala.Tuple2;@33f82290, tomcat -> [Lscala.Tuple2;@67a53697, hello -> [Lscala.Tuple2;@fa328aa, word -> [Lscala.Tuple2;@20c2b8eb)
val arr2=arr1.groupBy(t=>t._1)
//Map(good -> 1, jeryy -> 1, ketty -> 1, tomcat -> 2, hello -> 3, word -> 1)
val arr3=arr2.map(t=>(t._1,t._2.length))
//List((gooa,1), (jeryy,1), (ketty,1), (word,1), (tomcat,2), (hello,3))
val arr4=arr3.toList.sortBy(t=>t._2)
for(i <- 0 to arr4.length-1){
println(arr4(i))
}
}
}