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 }
好看请赞,养成习惯:) 本文来自博客园,作者:靠谱杨, 转载请注明原文链接:https://www.cnblogs.com/rainbow-1/p/15827938.html
欢迎来我的51CTO博客主页踩一踩 我的51CTO博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地