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
相关推荐
Chester_19993 小时前
CSP202203C.计算资源调度器EXI-小洲4 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载会周易的程序员4 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计机器视觉知识推荐、就业指导5 小时前
为什么老说“纯 Qt 没啥就业市场”?telepan5 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?m0_695251495 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)大衛說6 小时前
《Python 从入门到精通》系列总览与学习路线可乐鸡翅yeah_7 小时前
hls.js 内存泄漏实战排查,直播 M3U8 长时间播放异常定位方案2601_962203517 小时前
Java进阶07集合(续)郑州光合科技余经理8 小时前
本地生活服务系统:模块边界与结算字段怎么拆