Scala

复制代码
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()//结束写入
      //把结果保存到一个新的文件中:

    }
  }
}
相关推荐
benchmark_cc2 小时前
如何用 Python + QuantDash 快速构建高胜率“配对交易(Pairs Trading)”策略?
开发语言·人工智能·python·pandas·量化交易·quantdash
程序员无隅3 小时前
Coding Agent 为什么压缩上下文后还能继续工作?上下文模块设计拆解
java·开发语言·数据库
Python+993 小时前
Java 枚举类(Enum)详解:从基础到高级应用
java·开发语言·python
二炮手亮子4 小时前
浅记java线程池
java·开发语言
rockey6274 小时前
基于AScript的JavaScript脚本语言发布啦
javascript·c#·.net·js·script
zmzb01034 小时前
C++课后习题训练记录Day157
开发语言·c++
dunge20264 小时前
2026年7月最新ChatGPT Plus / Pro 与 Codex:当 AI Agent 最新5.6版本来袭,必须理解事务、幂等与补偿
开发语言·人工智能·python
LuoCore4 小时前
AForge.Video.FFMPEG.dll加载失败解决指南
c#
吴可可1235 小时前
C#元组解构实现变量交换
c#
爱吃牛肉的大老虎5 小时前
rust基础之环境搭建
java·开发语言·rust