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
相关推荐
库克克17 分钟前
【C++】C++11 包装器function 与 绑定器 bind多加点辣也没关系26 分钟前
JavaScript|第30章:事件机制小大宇27 分钟前
python milvus 案例Gauss松鼠会38 分钟前
【GaussDB】GaussDB锁阻塞源头查询mingo_敏1 小时前
DeepAgents : 后端(Backends)mabing9931 小时前
Qt QMessageBox、QDialogButtonBox中英文翻译动态切换Sagittarius_A*1 小时前
【RCELABS】Level 17~18 —— PHP命令执行函数与环境变量注入冻柠檬飞冰走茶1 小时前
PTA基础编程题目集 7-19 支票面额(C语言实现)破碎的南瓜2 小时前
靶场中使用到的php函数以及每种漏洞的防御方式谁看我谁 狼家二丫2 小时前
局域网文件共享实战:从“账户被禁用”到成功互传文件