首页 > 代码库 > Scala编程入门---Map与Tuple

Scala编程入门---Map与Tuple

创建Map

//创建一个不可变的Map
val ages = Map("Leo" -> 30,"Jen" ->25,"Jack" ->23)
ages("Leo") =31
//创建一个可变的Map
val ages =scala.collection.mutable.Map("Leo" ->30,"Jen" ->25,"Jack" ->23)
ages("Leo") = 31
//使用另外一种方式定义Map元素
val ages = Map(("Leo",30),("Jen",25),("Jack",23))
//创建一个工的HashMap
val ages = new scala.collection.mutable.HashMap[String,int]

 

访问Map元素

//获取指定Key对应的value,如果key不存在,会报错
val leoAge = ages("Leo")
val leoAge = ages("leo")

//使用contains函数检查key是否存在
val leoAge = if(ages.contains("leo")) ages("leo") else 0

//getOrElse函数
val leoAge = ages.getOrElse("leo",0)

 

修改Map的元素

//更新Map的元素

ages("leo") =31

//增加多个元素

ages +=("Mike" -> 35,"Tom" 0> 40)

//移除元素

ages -="Mike"

//更新不可变的map

val ages2 = ages +("Mike" -> 36,"Tom" ->40)

//移除不可变的元素

val ages3 = ages-"Tom"

遍历Map

//遍历map的entrySet
for ((key,value) <- ages) printlin(key+"" +value)
//遍历Map的Key
for(key <-ages.keySet) println(key)
//遍历Map的value
for(value <- ages.value ) println(value)
//生成新map,反转Key和value
for((key,value) <- ages) yield (value,key)

Map的排序

//sortedMap可以自动对Map的可以排序
val ages = scala.collection.immutable.SortedMap("leo" ->30,"alice" ->15,"jen" ->25)
//LinkedHashMap可以记住插入entry的顺序
val ages =new scala.collection.mutable.LinkedHashMap[String,int]
ages("leo")= 30
ages("alice")=15
ages("jen")=25

 

Tuple

//简单Tuple
val t =("leo",30)
//访问Tuple
t._1
    
//zip操作
val names =Array("leo","jack"," mike")
val ages = Array(30,23,26)
val nameAges = names.zip(ages)
for((name,age) <- nameAges) println(name +":"+age)

 

Scala编程入门---Map与Tuple