scala基础41-Set/Map/TreeSet/TreeMap

本文介绍了Scala中集合的基本操作,包括可变Set与List的交互、Map的使用、TreeSet及TreeMap的应用等。通过示例代码展示了如何进行增删改查等常见操作,并解释了这些集合的特点。
import scala.collection.mutable
import scala.collection.mutable.TreeSet
import scala.collection.immutable.TreeMap

object Test extends App{
  //可变Set和List互操作
  val data = mutable.Set.empty[Int]
  data ++= List(1, 2, 3)
  data += 4;
  data --= List(2, 3)
  println(data)

  //Set是不可重复的,因此下面的操作不起作用
  data += 1
  println(data)

  //清空Set
  data.clear()
  println(data)

  //map操作
  val map = mutable.Map.empty[String, String]
  map("java") = "hadoop"
  map("scala") = "spark"
  println(map)
  println(map("scala"))

  //treeSet操作,要求内容可排序
  val treeSet = TreeSet(9, 3, 6, 1)
  println(treeSet) //打印时自动排序
  val treeSetForChar = TreeSet("spark", "scala", "hadoop")
  println(treeSetForChar)

  //treeMap操作,要求内容可排序
  var treeMap = TreeMap("scala" -> "spark", "java" -> "hadoop")
  println(treeMap) //打印时自动排序
}

我这个是在Ubuntu运行sbt run发生的 root@Ubuntu:/home/rt/chisel-examples/hello-world# sbt run [info] welcome to sbt 1.10.11 (Ubuntu Java 21.0.6) [info] loading project definition from /home/rt/chisel-examples/hello-world/project [info] loading settings for project hello-world from build.sbt... [info] set current project to hello-world (in build file:/home/rt/chisel-examples/hello-world/) [info] compiling 1 Scala source to /home/rt/chisel-examples/hello-world/target/scala-2.12/classes ... [info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.15. Compiling... error: bad constant pool index: 0 at pos: 48454emental 2s while compiling: <no file> during phase: globalPhase=<no phase>, enteringPhase=<some phase> library version: version 2.12.15 compiler version: version 2.12.15 reconstructed args: -bootclasspath /root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.15/scala-library-2.12.15.jar -nowarn -classpath /root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-sbt/compiler-interface/1.10.8/compiler-interface-1.10.8.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-sbt/util-interface/1.10.7/util-interface-1.10.7.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-sbt/compiler-bridge_2.12/1.10.8/compiler-bridge_2.12-1.10.8-sources.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.15/scala-compiler-2.12.15.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.15/scala-reflect-2.12.15.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_2.12/1.0.6/scala-xml_2.12-1.0.6.jar:/root/.cache/coursier/v1/https/repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.jar -d /tmp/sbt_bc12c207 -Xmaxwarns 0 last tree to typer: EmptyTree tree position: <unknown> tree tpe: <notype> symbol: null call site: <none> in <none> =
03-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值