package test_30.test_32
import java.io.PrintWriter
import scala.io.Source
object test {
def main(args: Array[String]): Unit = {
val content=Source.fromFile("1.txt").mkString
println(content)
//把字符串中的每个单词,找出来
//正
val arr=content.split("\\W+")//得到是一个字符串数组
arr.foreach(println)
val arr1=Array("thank","you","very","much","thank","very")
val m1=scala.collection.mutable.Map[String,Int]()
arr1.foreach(word => {
//检查是否出现过
if (m1.contains(word)) {
//把票数+1
m1(word) += 1
}else{
//票数为1
m1(word)=1
}
})
//对于m1,他是一个Map(thank:10,is:5),现在需要对他进行排序,把出现次数最多的放在最前面,然后在输出
//(thank,10)
//只要排序之前的结果的前15个?在List中取出前15个?
val sortedM1=m1.toList.filter(a => a._1.length > 2).sortWith((a, b) => a._2 > b._2).slice(0,15)
//开始写入
val writer=new PrintWriter("3.txt")
//输出排序后的Map
//把结果保存到一个新的文件中
writer.println("统计的结果是:")
sortedM1.foreach{case (word,cishu)=> println(s"${word}: ${cishu}")
writer.close()//结束写入
//把结果保存到一个新的文件中:
}
}
}
Scala
爱吃香菜---www2024-11-28 19:18
相关推荐
timeDifference₇13 分钟前
Python Vibe Coding实操实验报告geovindu1 小时前
python:DecisionTreerockey6272 小时前
AScript正式推出Template模板引擎geovindu3 小时前
python: Decision Tree Regressor沐言Agent4 小时前
做工业上位机,这个 C# 开源框架把 PLC 通讯、流程控制和 MES 对接都备好了炸膛坦客6 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针夜不会漫长6 小时前
C++:C++11特性apcipot_rain8 小时前
C++转java日记(13)郝学胜-神的一滴8 小时前
Effective Python 条款41:试Mix‑In组合式开发吃我一窝jio9 小时前
Python与Redis集合深度解析