Scala-18:简单的WordCount案例

Scala-18:简单的WordCount案例

一、案例分析

对于传过来得一个数据,比如一个列表

List(
      "hello",
      "hello world",
      "hello scala",
      "hello spark from scala",
      "hello flink from scala"
    )

要统计每个单词出现的次数,输出的结果应该是

hello, 5
world, 1
scala, 3
spark, 1
flink, 1
from, 2

然后按照降序排序,取出现次数最高的三位

那么具体的实现步骤就可以是:

  1. 获取每一行的数据,对每一行的数据进行切分,获得每一个单词

    "hello world" => hello, world
    
  2. 对每一个单词进行分组,相同的单词分到一组

    Map[String, LIst[String]] = (hello => (hello,hello,hello,hello,hello))
    
  3. 对分组之后的list取长度,转换为单词加上出现的次数

    Map[String, LIst[String]] => Map[(String,Int)]
    
  4. 上个步骤的数据输出为map类型,转换为list类型,按照出现的次数降序排序,然后取前三

    Map[(String,Int)] => List[(String,Int)]
    

二、所用方法

1:转换/映射方法 map()

将集合中的每一个元素映射到某一个函数

list.map()

2:切分字符串 split()

//按照空格切分
list.map(_.split(" "))

3:扁平化处理 flatten()

list.flatten()

4:列表分组 groupBy()

//按照单词分组
list.groupBy(word => word)

5:map转为lsit toList()

map.toList

6:排序

//按照单词出现的次数排序
list.sortWith(_._2 > _._2)

三、代码实现

object CommonWorldCount {
  def main(args: Array[String]): Unit = {

    val stringList: List[String] = List(
      "hello",
      "hello world",
      "hello scala",
      "hello spark from scala",
      "hello flink from scala"
    )

    //1.对字符串进行切分,得到一个打散的列表
    val wordList1: List[Array[String]] = stringList.map(_.split(" "))
    val wordList2: List[String] = wordList1.flatten

    //2.相同的单词进行分组
    val groupMap: Map[String, List[String]] = wordList2.groupBy(word => word)

    //3.对分组之后的list取长度,得到每个单词的个数
    val countMap: Map[String, Int] = groupMap.map(kv => (kv._1, kv._2.length))

    //4.转换为list,并排序取前三
    //转换为list
    val countList: List[(String, Int)] = countMap.toList
    //排序
    val sortList: List[(String, Int)] = countList.sortWith(_._2 > _._2)
    //取前三
    val preList = sortList.take(3)
    println(preList)

    //可以简化为一步
    val preList2 = countMap.toList
      .sortWith(_._2 > _._2)
      .take(3)
  }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牧码文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值