Scala入门学习之【wordcount】

 val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")

    println(lines.flatMap(_.split(" ")))
    //List(hello, tom, hello, jerry, hello, jerry, hello, kitty)
    //flatMap中的_表示一行内容 hello tom hello jerry  一共有三行  即对每行操作
    //flatMap(_.split(" ")) 中的_.split(" ") 就相当于"hello tom hello jerry".split(" ")


    println(lines.flatMap(_.split(" ")).map((_ ,1)))
    //List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (jerry,1), (hello,1), (kitty,1))
    //返回的List集合,集合里边每一个元素为元组  访问元组的第一个元素为_._1
    //例如:println((""hello",1)._1)  结果为hello
    //lines.flatMap(_.split(" ")).map((_ ,1))中的map((_ ,1) _表示每一个单词,1表示每出现一次计数为1



    println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1))
    //Map(tom -> List((tom,1)), kitty -> List((kitty,1)), jerry -> List((jerry,1), (jerry,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1)))
   // lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1)中的groupBy(_._1)表示按照list中每个元组中的第一个字段分组即拿第一个字段作为key,返回结果是一个大Map
    //groupBy(_._1)中的第一个_表示list中的每一个元组,而  ._1  表示取每一个元组中的第一个元素




    println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)))
    //Map(tom -> 1, kitty -> 1, jerry -> 2, hello -> 4)
   // lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues()中的mapValues()仅仅会对value处理,处理完了把key 结合起来
    //  mapValues()中的第一个_表示map里边的value ,而value是一个list
    //lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))  中的foldLeft(0)是给一个初始值
     // (_+_._2)中的第一个_表示初始值或者累加过的值,第二个_表示List里边的元组,._2表示拿到元组中的第二个字段




    println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList)
   // List((tom,1), (kitty,1), (jerry,2), (hello,4))
    // 转化为List



    println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2))
  //List((tom,1), (kitty,1), (jerry,2), (hello,4))
    //sortBy(_._2)中的第一个_ 表示每一个元组,第二个._2 每个元组中的第二个字段



    println(lines.flatMap(_.split(" ")).map((_ ,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2)).toList.sortBy(_._2).reverse)
    //List((hello,4), (jerry,2), (kitty,1), (tom,1))
    //reverse表示降序排序

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值