美图欣赏:
一.背景
映射(map), 也是十分重要的。大好基础,才能一路打boss
二.Map介绍
在Scala中,把哈希表这种数据结构叫做映射。
Scala中的Map存储的内容是键值对(key-value),Scala中不可变的Map是有序的,可变的Map是无序的。
Scala中,有可变Map (scala.collection.mutable.Map) 和
不可变Map(scala.collection.immutable.Map) 。
三.不可变Map集合
1.第一种,创建Map的方式
scala> val map = Map(("Jackson", 9),("Tom", 6),("Andy", 10))
map: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10)
scala> map
res0: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10)
2.第二种,创建Map的方式
scala> val map1 = Map("Lili" -> 20, "Plus" -> 23, "King" -> 19)
map1: scala.collection.immutable.Map[String,Int] = Map(Lili -> 20, Plus -> 23, King -> 19)
scala> map1("Lili")
res1: Int = 20
3.map里面contains包含方法,返回的是值
//包含例子
scala> if(map.contains("Jackson")) map("Jackson") else 0
res3: Int = 9
//不包含例子
scala> if(map.contains("Jackson1")) map("Jackson1") else 0
res4: Int = 0
4.map中的getOrElse,如果有则返回值,如果没有则返回自定义的
//返回的有
scala> map.getOrElse("Jackson", -1)
res5: Int = 9
返回的没有,-1是自己定义返回的值
scala> map.getOrElse("zhangsan", -1)
res6: Int = -1
5.这里定义的map是不可变的,所以没法添加,修改其中的key和value
scala> map("Jackson") = 100
<console>:13: error: value update is not a member of scala.collection.immutable.Map[String,Int]
map("Jackson") = 100
^
scala> map +=("Mack" -> 200)
<console>:13: error: value += is not a member of scala.collection.immutable.Map[String,Int]
map +=("Mack" -> 200)
^
三.可变Map集合
1.首先要导包
scala> import scala.collection.mutable._
import scala.collection.mutable._
2.创建Map
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val map1 = Map("Lili" -> 20, "Plus" -> 23, "King" -> 19)
map1: scala.collection.mutable.Map[String,Int] = Map(Lili -> 20, Plus -> 23, King -> 19)
scala> map1("Lili") = 100
scala> map1
res10: scala.collection.mutable.Map[String,Int] = Map(Lili -> 100, Plus -> 23, King -> 19)
3.添加Map
scala> map1 += ("Jack" -> 200)
res11: map1.type = Map(Lili -> 100, Jack -> 200, Plus -> 23, King -> 19)
4.Scala中的Map存储的内容是键值对(key-value),Scala中不可变的Map是有序的,可变的Map是无序的。
当添加一个key和value时,可见,并不是按顺序添加的
scala> map1
res12: scala.collection.mutable.Map[String,Int] = Map(Lili -> 100, Jack -> 200, Plus -> 23, King -> 19)
5.map中当添加相同的key时,无论value相同还是不相同,value都会覆盖
添加的key和value相同时,都会覆盖(例子)
scala> map1 +=("Plus"->23)
res13: map1.type = Map(Lili -> 100, Jack -> 200, Plus -> 23, King -> 19)
添加的key相同,value不相同时,都会覆盖(例子)
scala> map1 +=("Plus"->24)
res14: map1.type = Map(Lili -> 100, Jack -> 200, Plus -> 24, King -> 19)
6.+= 可以一次添加多个key和value
scala> map1 +=("zhangsan"->28 , "wangwu" -> 29)
res15: map1.type = Map(Lili -> 100, zhangsan -> 28, Jack -> 200, Plus -> 24, King -> 19, wangwu -> 29)
7.-=一次只能减去一个key和 value
scala> map1 -="zhangsan"
res16: map1.type = Map(Lili -> 100, Jack -> 200, Plus -> 24, King -> 19, wangwu -> 29)
8.remove一次只能减去一个key和 value
scala> map1.remove("wangwu")
res17: Option[Int] = Some(29)
scala> map1
res18: scala.collection.mutable.Map[String,Int] = Map(Lili -> 100, Jack -> 200, Plus -> 24, King -> 19)
四.不可变Map集合,可以模拟添加key和value 但原有的集合没有改变,可以生成另一个集合
scala> map
res20: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10)
1.操作符 +
scala> map + ("Mading" -> 15)
res21: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10, Mading -> 15)
2.原有的没有变
scala> map
res22: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10)
3.返回的一个新集合
scala> res21
res23: scala.collection.immutable.Map[String,Int] = Map(Jackson -> 9, Tom -> 6, Andy -> 10, Mading -> 15)
4.可以多个,添加时无序的
scala> map + ("Lee1" -> 15, "Lee2" -> 16)
res25: scala.collection.immutable.Map[String,Int] = Map(Tom -> 6, Lee2 -> 16, Andy -> 10, Jackson -> 9, Lee1 -> 15)
5.可以减去一个操作
操作符 -
scala> res25
res26: scala.collection.immutable.Map[String,Int] = Map(Tom -> 6, Lee2 -> 16, Andy -> 10, Jackson -> 9, Lee1 -> 15)
scala> res25 - "Lee1"
res27: scala.collection.immutable.Map[String,Int] = Map(Tom -> 6, Lee2 -> 16, Andy -> 10, Jackson -> 9)
scala> res27
res28: scala.collection.immutable.Map[String,Int] = Map(Tom -> 6, Lee2 -> 16, Andy -> 10, Jackson -> 9)
6.扩展:
spark中源码里面的, ,,_指的就是个占位符
scala> val (a,_,_,_) = (4,5,6,7)
a: Int = 4
//到时候直接可以拿b c d ,因此_,_,_指的就是个占位符
scala> val b = 5
b: Int = 5
scala> val c = 6
c: Int = 6
scala> val d = 7
d: Int = 7
循环然后取值
打印出所有的key和value
scala> for ((key, value) <- map ) println(key + " " + value)
Jackson 9
Tom 6
Andy 10
打印出map中所有keys
scala> for(key <- map.keys) println(key)
Jackson
Tom
Andy
打印出map中所有values
scala> for(value <- map.values) println(value)
9
6
10
7.使用yield 返回一个新的map
scala> val map2 = for((key , value) <- map) yield (value , key)
map2: scala.collection.immutable.Map[Int,String] = Map(9 -> Jackson, 6 -> Tom, 10 -> Andy)
8.immutable.SortedMap 按字典可以排序(针对的是key)
scala> val map3 = scala.collection.immutable.SortedMap("b" -> 3, "a" -> 8, "d" -> 5, "c" -> 4)
map3: scala.collection.immutable.SortedMap[String,Int] = Map(a -> 8, b -> 3, c -> 4, d -> 5)
9.LinkedHashMap 插入是有顺序的
scala> val map4 =new scala.collection.mutable.LinkedHashMap[String , Int]
map4: scala.collection.mutable.LinkedHashMap[String,Int] = Map()
scala> map4("z")=8
scala> map4("x")=9
scala> map4("y")=10
scala> map4
res38: scala.collection.mutable.LinkedHashMap[String,Int] = Map(z -> 8, x -> 9, y -> 10)
————保持饥饿,保持学习
Jackson_MVP