本文主要是介绍第8节:scala常用集合Map和Tuple,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package basicsobject HelloMapTuple {def main(args: Array[String]): Unit = {var bigdatas = Map("Spark" -> 6, "Hadoop" -> 11)//bigdatas("Spark")=10直接构造的map是不可变的,一旦改变就变成新的内容,原有内容保持不变 println(bigdatas.apply("Spark"))val progran = scala.collection.mutable.Map("Spark" -> 15, "java" -> 15)progran("Spark") = 10 for ((key, age) <- progran) println("key--"+key+":" + age)val progranhashmap = new scala.collection.mutable.HashMap[String, Int]progranhashmap.put("spk", 11)print(progranhashmap.getOrElse("Python","is null")) //为了不报异常或者提供默认值 val person = Map(("zyl", 28), ("dtspark", 1))// println(person.getOrElse("zyl", "dtspark")) progranhashmap += ("Spark" -> 6, "Hadoop" -> 11)// progranhashmap -= ("java") for (key <- progranhashmap.keySet) println(key)for (value <- progranhashmap.values) println(value)for ((name, age) <- progranhashmap) yield (age, name)println("----------------------------1")for ((name, age) <- progranhashmap) println(name + ":" + age)println("----------------------------2")//排序,默认会按照key的字母首字母排序 val sortMap = scala.collection.immutable.SortedMap(("eyl", 28), ("ftspark", 100))for ((name, age) <- sortMap) println(name + ":" + age)println("----------------------------*********")//LinkedHashMap可以记住元素插入的顺序 val linkedhashmap = new scala.collection.mutable.LinkedHashMap[String, Int]linkedhashmap += ("Spark" -> 6, "Hadoop" -> 11, "java" -> 12)for ((name, age) <- linkedhashmap) println(name + ":" + age)println()val tuple=("zyl",28,"java","I am into spark so much!!")//tuple从1k开始索引,tuple中可以有很多不同类型的数据 println(tuple._3)} }打印结果:
6
key--java:15
key--Spark:10
is nullspk
Hadoop
Spark
11
11
6
----------------------------1
spk:11
Hadoop:11
Spark:6
----------------------------2
eyl:28
ftspark:100
----------------------------*********
Spark:6
Hadoop:11
java:12
java
这篇关于第8节:scala常用集合Map和Tuple的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!