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

    }
  }
}
相关推荐
andyweike1 小时前
php笔记
开发语言·笔记·php
Circ.2 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
波力海苔夹心脆6753 小时前
C# 序列化与反序列化详解:System.Text.Json、Newtonsoft.Json、XmlSerializer 用法、特性选项与安全实践
经验分享·后端·c#·json·.net
现任明教教主~4 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411364 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅5 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
小小小小钰儿6 小时前
1.5-Python网络编程
开发语言·网络·python·web安全·计算机·网络安全
hiahiahia1236 小时前
实现完整 Tool Dispatcher
开发语言·前端