- 默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变;
- Map的实例是调用工厂方法模式apply来构造Map实例,而需要主要的是Map是接口,在apply中使用了具体的实现
// 调用工厂方法模式apply来构造Map实例,而需要主要的是Map是接口,在apply中使用了具体的实现
val bigDatas = Map("Spark" -> 5, "Hadoop" -> 11)
- 如果想直接new出Map实例,则需要使用HashMap等具体的Map子类;
- 查询一个Map中的值一定是采用getOrElse的语法,一方面是在key不存在的情况下不报告异常,另外还有一个作用就是提供默认值;而关于默认值的提供在实际开发中至关重要,在Spark中很多默认的配置都是通过getOrElse的方式来实现的;
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可以记住插入数据的顺序,这在实际开发中非常有用;
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中可以有很多不同类型的数据,例如("yaj", "Spark", "30", "I LOVE SCALA!!!")
- 在企业级实际开发大数据的时候一定会反复的使用Tuple来表达数据结构,以及使用Tuple来处理业务逻辑
val info = ("yaj", "Spark", "30", "I LOVE SCALA!!!")
println(info._4)
- Tuple的另外一个非常重要的使用是作为函数的返回值,在Tuple中返回若干个值,以SparkContext源码为例来说明
完整的实例如下:
object HelloMapTuple {
def main(args: Array[String]): Unit = {
// 调用工厂方法模式apply来构造Map实例,而需要主要的是Map是接口,在apply中使用了具体的实现
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)
}