您的位置:首页 > 其它

Scala分步完成wordcount例子

2017-08-20 00:00 253 查看
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))
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: