一、Map实战
1.Map定义
val map = Map("spark"->18,"scala"->17,"hadoop"->18) //> map : scala.collection.immutable.Map[String,Int] = Map(spark -> 18, scala -> 17, hadoop -> 18)
for((k,v) <- map) yield (k,v * 0.9) //> res0: scala.collection.immutable.Map[String,Double] = Map(spark -> 16.2, scala -> 15.3, hadoop -> 16.2)
2.getOrElse
val scores = scala.collection.mutable.Map("spark"->16,"scala"->17,"hadoop"->18)
//> scores : scala.collection.mutable.Map[String,Int] = Map(hadoop -> 18, spark-> 16, scala -> 17)
val hadoopScore = scores.getOrElse("spark",100) //> hadoopScore : Int = 18
如果Map里有“spark”,那么返回其对于的value,如果没有的话,返回get