Scala 简单分词求和

复制代码
 1 package chapter07
 2 
 3 object Test17_CommonWordCount {
 4   def main(args: Array[String]): Unit = {
 5     val stringList: List[String] = List(
 6       "hello",
 7       "hello world",
 8       "hello scala",
 9       "hello spark from scala",
10       "hello flink from scala"
11     )
12 
13     // 1. 对字符串进行切分,得到一个打散所有单词的列表
14 //    val wordList1: List[Array[String]] = stringList.map(_.split(" "))
15 //    val wordList2: List[String] = wordList1.flatten
16 //    println(wordList2)
17     val wordList = stringList.flatMap(_.split(" "))
18     println("分词 :"+wordList)
19 
20     // 2. 相同的单词进行分组
21     val groupMap: Map[String, List[String]] = wordList.groupBy(word => word)
22     println("分组 :"+groupMap)
23 
24     // 3. 对分组之后的list取长度,得到每个单词的个数
25     val countMap: Map[String, Int] = groupMap.map(kv => (kv._1, kv._2.length))
26 
27     // 4. 将map转换为list,并排序取前3
28     val sortList: List[(String, Int)] = countMap.toList
29       .sortWith( _._2 > _._2 )    //排序 取下划线2 _2 
30       .take(3)    //取当前列表的前三名
31 
32     println(sortList)
33   }
34 }
复制代码

 

posted @   靠谱杨  阅读(107)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-01-20 1.20

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示