- 默認(rèn)情況下Map構(gòu)造的是不可變的集合涨冀,里面的內(nèi)容不可修改船惨,一旦修改就變成新的Map,原有的Map內(nèi)容保持不變擂错;
- Map的實(shí)例是調(diào)用工廠方法模式apply來(lái)構(gòu)造Map實(shí)例味滞,而需要主要的是Map是接口,在apply中使用了具體的實(shí)現(xiàn)
// 調(diào)用工廠方法模式apply來(lái)構(gòu)造Map實(shí)例钮呀,而需要主要的是Map是接口剑鞍,在apply中使用了具體的實(shí)現(xiàn)
val bigDatas = Map("Spark" -> 5, "Hadoop" -> 11)
- 如果想直接new出Map實(shí)例,則需要使用HashMap等具體的Map子類爽醋;
- 查詢一個(gè)Map中的值一定是采用getOrElse的語(yǔ)法蚁署,一方面是在key不存在的情況下不報(bào)告異常,另外還有一個(gè)作用就是提供默認(rèn)值蚂四;而關(guān)于默認(rèn)值的提供在實(shí)際開發(fā)中至關(guān)重要光戈,在Spark中很多默認(rèn)的配置都是通過getOrElse的方式來(lái)實(shí)現(xiàn)的哪痰;
val programingLanguage = scala.collection.mutable.Map("Scala" -> 13, "Java" -> 23)
programingLanguage("Scala") = 10
for((name, age) <- programingLanguage) println(name + " : " + age)
println(programingLanguage.getOrElse("Python", "123"))
- 使用SortedMap可以得到排序的Map集合;
val persons = scala.collection.mutable.SortedMap(("yaj", 28), ("yrz", 4),("xh", 30))
for((age, name) <- persons) println(name + " : " + age)
- LinkedHashMap可以記住插入數(shù)據(jù)的順序久妆,這在實(shí)際開發(fā)中非常有用晌杰;
val personsInformation2 = new scala.collection.mutable.LinkedHashMap[String, Int]
personsInformation2 += ("Scala" -> 13, "Java" -> 23, "Python" -> 10)
for((name, age) <- personsInformation2) println(name + " : " + age)
val personsInformation = new scala.collection.mutable.HashMap[String, Int]
personsInformation += ("Scala" -> 13, "Java" -> 23)
personsInformation -= ("Scala")
for((name, age) <- personsInformation) println(name + " : " + age)
for(key <- personsInformation.keySet) println(key)
for(value <- personsInformation.values) println(value)
println(personsInformation.keySet)
println(personsInformation.values)
- Tuple中可以有很多不同類型的數(shù)據(jù),例如("yaj", "Spark", "30", "I LOVE SCALA!!!")
- 在企業(yè)級(jí)實(shí)際開發(fā)大數(shù)據(jù)的時(shí)候一定會(huì)反復(fù)的使用Tuple來(lái)表達(dá)數(shù)據(jù)結(jié)構(gòu)镇饺,以及使用Tuple來(lái)處理業(yè)務(wù)邏輯
val info = ("yaj", "Spark", "30", "I LOVE SCALA!!!")
println(info._4)
- Tuple的另外一個(gè)非常重要的使用是作為函數(shù)的返回值乎莉,在Tuple中返回若干個(gè)值,以SparkContext源碼為例來(lái)說(shuō)明
完整的實(shí)例如下:
object HelloMapTuple {
def main(args: Array[String]): Unit = {
// 調(diào)用工廠方法模式apply來(lái)構(gòu)造Map實(shí)例奸笤,而需要主要的是Map是接口惋啃,在apply中使用了具體的實(shí)現(xiàn)
val bigDatas = Map("Spark" -> 5, "Hadoop" -> 11)
val programingLanguage = scala.collection.mutable.Map("Scala" -> 13, "Java" -> 23)
programingLanguage("Scala") = 10
for((name, age) <- programingLanguage) println(name + " : " + age)
println(programingLanguage.getOrElse("Python", "123"))
val personsInformation = new scala.collection.mutable.HashMap[String, Int]
personsInformation += ("Scala" -> 13, "Java" -> 23)
personsInformation -= ("Scala")
for((name, age) <- personsInformation) println(name + " : " + age)
for(key <- personsInformation.keySet) println(key)
for(value <- personsInformation.values) println(value)
println(personsInformation.keySet)
println(personsInformation.values)
val result = for((name, age) <- personsInformation) yield (age, name)
for((age, name) <- result) println(name + " : " + age)
val persons = scala.collection.mutable.SortedMap(("yaj", 28), ("yrz", 4),("xh", 30))
for((age, name) <- persons) println(name + " : " + age)
val personsInformation2 = new scala.collection.mutable.LinkedHashMap[String, Int]
personsInformation2 += ("Scala" -> 13, "Java" -> 23, "Python" -> 10)
for((name, age) <- personsInformation2) println(name + " : " + age)
val info = ("yaj", "Spark", "30", "I LOVE SCALA!!!")
println(info._4)
}