scala中的map其实和java类似,也是散列表, 不可变map是有序的,可变map是无序的。 immutable.Map: val:是自身不可变的,只能生成新的map; var:类似于可变集合,可以使用 += ++= (var修饰的map对象使用+=等方法,生成了新map,引用转变) mutable.Map: val:直接修改,无序性造成线程安全问题 注意:var和val只是引用是否可变, 即指针引用是否可变,和map是否可变无关
import scala.collection._
//不可变集合:低耗高效 安全
//val 修饰 + - ++ --
val s1= immutable.Map("aaa0" -> "bbb0")
val s12 = s1.+("aa" -> "bb")
println("1-新的不可变集合:"+s12)
//var 修饰 += ++= -= --=
var s2 = immutable.Map("aaa1" -> "bbb1")
s2 += ("qqq1"->"aaa1")
s2 -="qqq1"
println("2-原不可变集合:"+s2)
//可变集合 += ++= -= --=
val s3= mutable.Map("aaa2" -> "bbb2")
val s4=mutable.Map("aaa4" -> "bbb4")
s3+=("qqq2"->"aaa2")
println("3-可变集合:"+s3)
val string = s3 ++= s4
println("4-合并后的集合:"+string)
//java map <=>scala map
import scala.collection.JavaConverters._
val java = s3.asJava
println("5-java map"+java)
val scala1= java.asScala
println("6-scala mutableMap"+scala1)
//imu<=>mu 默认不可变 强类型语言自动推断,idea设置自动显示类型或利用isInstanceOf判断
val s6= s3.toMap //im
val s7= mutable.Map.empty ++ s6 //mu
println("7-判断:"+s7.isInstanceOf
[mutable.Map[String,String]])
//自动排序 collection.SortedMap scala.collection.immutable.TreeMap
val s8= SortedMap("a" -> "c", "v" -> "s", "b" -> "s")
val s9 = immutable.TreeMap("a" -> "c", "v" -> "s", "b" -> "s")
//set->map zip zipWithIndex
val strings = Set("A", "s")
val m1= strings.zip(Stream.from(1)).toMap
val m2= strings.zipWithIndex
.map(x=>(x._1,x._2+1)).toMap //原始从0开始
//scala中使用java map 手动导包import java.util
val javaMap = new util.HashMap[String,util.HashMap[String, String]]()
//生产小例子
if(!javaMap.containsKey("aaa"))
javaMap.put("aaa",new util.HashMap[String,String]())
val javaMap1 = javaMap.get("aaa")
if(!javaMap1.containsKey("bbb"))
javaMap1.put("bbb","sss")
println("8-javaMap:"+javaMap)
//scala mutable map
val scalaMap= mutable.Map[String,mutable.Map[String,String]]()
if(!scalaMap.contains("aaa1"))
//Map[String,String]()不能省略()
scalaMap+=("aaa1"->mutable.Map[String,String]())
val scalaMap1 = scalaMap("aaa1")
if(!scalaMap1.contains("bbb1"))
scalaMap1+=("bbb1"->"sss1")
println("9-scalaMap:"+scalaMap)
// 单层map
//get(key)=>option[类型] 对象 防止报错:NoSuchElementException
// 无对应key为None 有对应key 使用get方法获取对象里面的值
val map = Map("A" -> "SS")
val maybeString = map.get("A")
if(maybeString.nonEmpty){
val value = maybeString.get
println("10-option里面的值:"+value)
}
//多层同层次map
val sss = Map("aaa" -> Map("bbb" -> "ccc"),
"aaa1" -> Map("bbb1" -> "ccc"),
"aaa2" -> Map("bbb2" -> "ccc"))
val str = sss("aaa")("bbb")
println("11-1 map: "+str)
if(sss.contains("aaa")){
val stringToString5 = sss("aaa")
if(stringToString5.contains("bbb")){
val maybeString1 = stringToString5("bbb")
println("11-2 map生产: "+maybeString1)
}
}
val mst = sss.get("aaa")
if(mst.nonEmpty){
val maybeString1 = mst.get.get("bbb")
if(maybeString1.nonEmpty){
val value = maybeString1.get
println("11-3 map: "+value)
}
}
打印结果如下:
1-新的不可变集合:Map(aaa0 -> bbb0, aa -> bb)
2-原不可变集合:Map(aaa1 -> bbb1)
3-可变集合:Map(qqq2 -> aaa2, aaa2 -> bbb2)
4-合并后的集合:Map(qqq2 -> aaa2, aaa2 -> bbb2, aaa4 -> bbb4)
5-java map{qqq2=aaa2, aaa2=bbb2, aaa4=bbb4}
6-scala mutableMapMap(qqq2 -> aaa2, aaa2 -> bbb2, aaa4 -> bbb4)
7-判断:true
8-javaMap:{aaa={bbb=sss}}
9-scalaMap:Map(aaa1 -> Map(bbb1 -> sss1))
10-option里面的值:SS
11-1 map: ccc
11-2 map生产: ccc
11-3 map: ccc
本文含有隐藏内容,请 开通VIP 后查看