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
相关推荐
方也_arkling4 小时前
【Java-Day08】static / final / 枚举风吹夏回4 小时前
Python 全局异常处理:从“满屏 try-except”到优雅兜底Chengbei114 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息llz_1124 小时前
web-第一次课后作业小熊Coding4 小时前
Python爬取当当网二手图书项目实战!秋94 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案xiaoshuaishuai85 小时前
C# 内存管理与资源泄漏lsx2024065 小时前
SVN 检出操作basketball6166 小时前
C++ NULL 和 nullptr 区别 以及 nullptr 的核心实现旺仔来了6 小时前
不联网的Linux下部署python环境