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

    }
  }
}
相关推荐
程序员-Benothing1 小时前
如何实现高并发系统订单30分钟自动关闭?
开发语言·c#·wpf
郝学胜-神的一滴2 小时前
Python 高级编程 026:内置数据结构之骈文纵论
开发语言·数据结构·python·程序人生·软件工程
隐积10 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
名字还没想好☜12 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏13 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
海盗123413 小时前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行14 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr82814 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农16 小时前
功能测试--Day07--Python编程基础
开发语言·python
fengci.16 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php