码迷,mamicode.com
首页 > 其他好文 > 详细

scala基础(1) -- map

时间:2019-08-24 20:44:49      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:size   color   create   reac   efi   tput   number   cal   use   

 

creates Map

val weights = Map("cat" -> 10, "elephant" -> 200000)
val weight = weights("elephant")
println(weight)

 

val colors = Map(("bird", "blue"), ("fox", "red"))
val result1 = colors("bird")
println(result1)

 adds key, value to map

val zoo = Map("frog" -> 1, "lion" -> 1)
val all = zoo + ("zebra" -> 1)

 

iterate over all values.

zoo.keys.foreach(println(_))
for (value <- zoo.values) {
    println(value)
}

 

getOrElse

// Use a default value if the key does not exist.
val result3 = sizes.getOrElse("Small", 0)
if (result3 == 0) {
    println("Zero, else value")
}

 

val result2 = sizes.get("Large")
if (result2.isDefined) {
    println(result2)
    val number = result2.get
    println(number)
}

 equals

val map1 = Map((10, true), (20, false))
val map2 = Map((20, false), (10, true))
val map3 = Map((20, true), (10, true))

// These two maps are structurally equal.
if (map1.equals(map2)) {
    println("Maps equal")
}

 Output

Maps equal

 

scala基础(1) -- map

标签:size   color   create   reac   efi   tput   number   cal   use   

原文地址:https://www.cnblogs.com/pocahontas/p/11405678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!