默认Map是不可变的Map
val map=Map("book"->10,"gun"->18,"ipad"->1000)
//map: scala.collection.immutable.Map[String,Int] = Map(book -> 10, gun -> 18, ipad -> 1000)</span>
构造一个可以变的Map[String,Int],其值是可以变化的
val bmap=scala.collection.mutable.Map("book"->10,"gun"->18,"ipad"->1000)
通过已有Map创建Map
val d=for((k,v) <- map) yield (k,v*2)
//d: scala.collection.immutable.Map[String,Int] = Map(book -> 20, gun -> 36, ipad-> 2000)</span>
val b=d.getOrElse("book",0)
//b: Int =20
val bmap=scala.collection.mutable.Map("book"->10,"gun"->18,"ipad"->1000)
val mybook=bmap.getOrElse("book",0)
//mybook: Int = 10
bmap+=("gpl"->66)
//res0: bmap.type = Map(book -> 10, gun -> 18, ipad -> 1000, gpl -> 66)
bmap-="gpl"
//res1: bmap.type = Map(book -> 10, gun -> 18, ipad -> 1000)
val sortedMap=scala.collection.immutable.SortedMap("scala"->7,"hadoop"->8,"spark"->9)
//sortedMap: scala.collection.immutable.SortedMap[String,Int] = Map(hadoop -> 8, scala -> 7, spark -> 9)//按key排序</span>
Tuple:
元组(tuple) 中可以是不同类型的值
val tuple=(100,"gpl","yl",'a')
//print(tuple._1+" "+tuple._2+" "+tuple._3)
print(tuple._1)
print(tuple._2)
print(tuple._3)
val tuple=(1,2,3,14,"scala","spark")
val third=tuple._3
//third: Int = 3
val (first,second,third,fourth,fifth,six)=tuple
val (first,second,third,fourth,_,_)=tuple
val s="Rocky Spark".partition(_.isUpper)
//s: (String, String) = (RS,ocky park)
zip:
//zip
val symbols=Array("1","4","2")
val counts=Array(2,5,2)
val pairs=symbols.zip(counts)
pairs: Array[(String, Int)] = Array((1,2), (4,5), (2,2))