Set、Map、TreeSet、TreeMap

def main(args: Array[String]) {
    
    val data = mutable.Set.empty[Int] //创建空的Set
    data ++= List(1, 2, 3) //++=添加多于一个元素的集合
    data += 4;
    data --= List(2, 3); //去除多于一个元素的集合
    println(data) //结果:Set(1, 4)
    data += 1;
    println(data) //依然是Set(1, 4),Set里面元素是不允许重复的
    data.clear //把Set中元素清空
    println(data)
    
    val map = mutable.Map.empty[String, String] //创建空的Map,并制定ley、value类型
    map("Java") = "Hadoop" //存入键值对
    map("Scala") = "Spark"
    println(map) //结果:Map(Scala -> Spark, Java -> Hadoop)
    println(map("Scala"))
      
    val treeSet = TreeSet(9, 3, 1, 8, 0, 2, 7, 4, 6, 5)  //排序Set(前提是里面的元素是可排序的,Ordering)
    println(treeSet) //结果:TreeSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    val treeSetForChar = TreeSet("Spark", "Scala", "Hadoop") 
    println(treeSetForChar) //结果:TreeSet(Hadoop, Scala, Spark)
    
    var treeMap = TreeMap("Scala" -> "Spark", "Java" -> "Hadoop") //对key进行排序
    println(treeMap) //结果:Map(Java -> Hadoop, Scala -> Spark)
    
  }

你可能感兴趣的:(Set、Map、TreeSet、TreeMap)