Scala学习笔记26【Set、Map、TreeSet、TreeMap实战】

package com.yl.scala

import scala.collection.mutable
import scala.collection.mutable.TreeSet
import scala.collection.immutable.TreeMap

object MapSet {
  def main(args: Array[String]){

    //Set实战
    val data = mutable.Set.empty[Int]
    data ++= List(1,2,3,4)
    println("data = " + data) //data = Set(1, 2, 3, 4)
    data += 9
    println("data = " + data) //data = Set(9, 1, 2, 3, 4)
    data --= List(2,3)
    println("data = " + data) //data = Set(9, 1, 4)
    data += 6
    println("data = " + data) //data = Set(9, 1, 6, 4)
    data.clear
    println("data = " + data) //data = Set()

    //Map实战
    val map = mutable.Map.empty[String, String]
    map("Java") = "Hadoop"
    map("Scala") = "Spark"
    println("map = " + map) //map = Map(Scala -> Spark, Java -> Hadoop)
    println(map("Scala"))  //Spark

    //TreeSet实战
    val treeSet = TreeSet(1,3,4,2,5,7,6,9,8,5,5,5)
    println("treeSet = " + treeSet)  //treeSet = TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val treeSetForChar = TreeSet("Java", "Scala", "Scala", "Hadoop", "Spark")
    println("treeSetForChar = " + treeSetForChar) //treeSetForChar = TreeSet(Hadoop, Java, Scala, Spark)

    //TreeMap实战
    var treeMap = TreeMap("Scala" -> "Spark", "Java" -> "Hadoop")
    println("treeMap = " + treeMap) //treeMap = Map(Java -> Hadoop, Scala -> Spark)
  }
}

运行结果:

data = Set(1, 2, 3, 4)
data = Set(9, 1, 2, 3, 4)
data = Set(9, 1, 4)
data = Set(9, 1, 6, 4)
data = Set()
map = Map(Scala -> Spark, Java -> Hadoop)
Spark
treeSet = TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9)
treeSetForChar = TreeSet(Hadoop, Java, Scala, Spark)
treeMap = Map(Java -> Hadoop, Scala -> Spark)

你可能感兴趣的:(scala,map,tree,TreeMap,TreeSet)