Scala學習筆記04_Map與Tuple
創建Map
// 創建一個不可變的Mapscala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)scala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)scala> ages("Leo") = 31<console>:15: error: value update is not a member of scala.collection.immutable.Map[String,Int] ages("Leo") = 31 ^// 創建一個可變的Mapscala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)scala> ages("Leo") = 31scala> ages("Leo")res43: Int = 31scala> agesres44: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 31)// 使用另外一種方式定義Map元素scala> val ages = Map(("Leo",30), ("Jen",25), ("Jack",23))ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)// 創建一個空的HashMapscala> val ages = new scala.collection.mutable.HashMap[String,Int]ages: scala.collection.mutable.HashMap[String,Int] = Map()scala> val ages = new scala.collection.mutable.HashMap[String,Int]()ages: scala.collection.mutable.HashMap[String,Int] = Map()scala> val ages = new scala.collection.mutable.Map[String,Int]()<console>:13: error: trait Map is abstract; cannot be instantiated val ages = new scala.collection.mutable.Map[String,Int]() ^
訪問Map的元素
// 獲取指定key對應的value,如果key不存在,會報錯scala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)scala> val leoAge = ages("Leo")leoAge: Int = 30scala> val leoAge = ages("leo")java.util.NoSuchElementException: key not found: leo at scala.collection.MapLike$class.default(MapLike.scala:228) at scala.collection.AbstractMap.default(Map.scala:59) at scala.collection.mutable.HashMap.apply(HashMap.scala:65) ... 32 elided// 使用contains函數檢查key是否存在scala> val leoAge = if(ages.contains("leo")) ages("leo") else 0leoAge: Int = 0// getOrElse函數scala> val leoAge = ages.getOrElse("leo",0)leoAge: Int = 0
修改Map的元素
// 更新Map的元素scala> agesres45: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)scala> ages("Leo") = 31scala> agesres47: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 31)// 增加多個元素scala> ages += ("Mike" -> 35, "Tom" -> 50)res48: ages.type = Map(Jen -> 25, Mike -> 35, Tom -> 50, Jack -> 23, Leo -> 31)// 移除元素scala> ages -= "Mike"res49: ages.type = Map(Jen -> 25, Tom -> 50, Jack -> 23, Leo -> 31)// 創建不可變的Mapscala> val ages = Map("Leo" -> 30, "Jack" -> 40, "Jen" -> 25)ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jack -> 40, Jen -> 25)// 更新不可變的Mapscala> val ages2 = ages + ("Mike" -> 36, "Tom" -> 60)ages2: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 60, Leo -> 30, Jack -> 40, Jen -> 25)// 原來Map已有的元素也可更新scala> val ages2 = ages + ("Leo" -> 31)ages2: scala.collection.immutable.Map[String,Int] = Map(Leo -> 31, Jack -> 40, Jen -> 25)// 移除不可變Map的元素scala> val ages3 = ages - "Jack"ages3: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25)
遍歷Map
// 遍歷Map的entrySetscala> agesres50: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jack -> 40, Jen -> 25)scala> for((key,value) <- ages) println(key + ": " + value)Leo: 30Jack: 40Jen: 25// 遍歷Map的keyscala> for(key <- ages.keySet) println(key)LeoJackJen// 遍歷Map的Valuescala> for(value <- ages.values) println(value)304025// 生成新Map,反轉key和valuescala> for((key, value) <- ages) yield (value, key)res54: scala.collection.immutable.Map[Int,String] = Map(30 -> Leo, 40 -> Jack, 25 -> Jen)
SortedMap和LinkedHashMap
// SortedMap可以自動對Map的key的排序scala> val ages = scala.collection.immutable.SortedMap("Leo" -> 30, "Jack" -> 20, "Jen" ->25)ages: scala.collection.immutable.SortedMap[String,Int] = Map(Jack -> 20, Jen -> 25, Leo -> 30)// LinkedHashMap可以記住插入entry的順序scala> val ages = new scala.collection.mutable.LinkedHashMap[String, Int]ages: scala.collection.mutable.LinkedHashMap[String,Int] = Map()scala> ages("leo") = 30scala> ages("jack") = 20scala> ages("jen") = 25scala> agesres3: scala.collection.mutable.LinkedHashMap[String,Int] = Map(leo -> 30, jack -> 20, jen -> 25)
Map的元素類型Tuple
// 簡單Tuplescala> val t = ("leo", 30)t: (String, Int) = (leo,30)// 訪問Tuplescala> t._1res4: String = leoscala> t._2res5: Int = 30// zip操作scala> val names = Array("leo", "jack", "jen")names: Array[String] = Array(leo, jack, jen)scala> val ages = Array(30, 20, 25)ages: Array[Int] = Array(30, 20, 25)scala> val nameAges = names.zip(ages)nameAges: Array[(String, Int)] = Array((leo,30), (jack,20), (jen,25))scala> for((name, age) <- nameAges) println(name + ": " + age)leo: 30jack: 20jen: 25
本文首發於steem,感謝閱讀,轉載請註明。
https://steemit.com/@padluo
微信公眾號「padluo」,分享數據科學家的自我修養,既然遇見,不如一起成長。
http://weixin.qq.com/r/P3WGnj3E82CMrXn99yAt (二維碼自動識別)
讀者交流電報群
https://t.me/sspadluo
知識星球交流群
推薦閱讀:
※還有必要學習Hadoop 么?
※實時處理中的"exactly once"方案具體的應用場景或者使用方法是什麼?
※Spark SQL 和 Shark 在架構上有哪些區別?將來會合併嗎?
※運行支持Kubernetes原生調度的Spark程序-Spark on Kubernetes
※Scala簡明語法