其他分享
首页 > 其他分享> > Scala 简单分词求和

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 }

 

标签:wordList,String,val,Scala,求和,List,hello,println,分词
来源: https://www.cnblogs.com/rainbow-1/p/15827938.html