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
相关推荐
caimouse26 分钟前
reactos编码规范xieliyu.4 小时前
Java算法精讲:双指针(三)CryptoPP5 小时前
快速对接东京证券交易所API数据:实战指南与代码示例ZC跨境爬虫6 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试阳区欠7 小时前
【LangChain】LLM基础介绍Jinkxs7 小时前
Java 跨域14-Java 与区块链(Hyperledger)集成晨曦中的暮雨8 小时前
Golang速通(Javaer版)小小编程路8 小时前
Python 还有容器类型互转、进制转换、字符编码转换qeen878 小时前
【C++】类与对象之类的默认成员函数(二)CRMEB系统商城8 小时前
CRMEB多商户系统(Java)v2.3公测版发布