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)