scala中Map和Set

scala中Set包含可变set和不可变Set,set的子类HashSet,各有一个扩展了可变和不可变的set特质。
 可变set
import scala.collection.mutable.Set
val  movieSet=Set("Hitch","Poltergeist")
    movieSet+="Shrek"
    println(movieSet)
// Set(Chilies, Tomatoes, Coriander)

 不可变set

import   scala.collection.immutable.HashSet
val  hashSet=HashSet("Tomatoes","Chilies")
    println(hashSet+"Coriander")
//  Set(Chilies, Tomatoes, Coriander

 map和set一样,scala采用了类继承机制提供了可变的和不可变的两种版本的Map,map的类继承机制看上去和set的很像,scala.collection包里面有一个基础Map特质和两个子特质Map:可变的Map在scala.collection.mutable里,。不可变的在scala.collection.immutable里

引入可变Map类 import scala.collection.mutable.Map
import scala.collection.mutable.Map
val treasureMap=Map[Int,String]()
treasureMap+=(1->"go to island.")
treasureMap+=(2->"Find big x on ground.")
treasureMap+=(3->"Dig.")
println(treasureMap(2))
 可变map不需要引用任何类,因为不可变map是默认的
 val romanNumeral=Map(1->"I",2->"II",3->"III",4->"IV")
println(romanNumeral)
原文地址:https://www.cnblogs.com/zwgblog/p/7171160.html