Scala分步完成wordcount例子

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))
    }
    
    
    
  }
}

转载于:https://my.oschina.net/u/3197158/blog/1517584

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值