scala集合三大類(seq序列,set集,map映射)——map映射

scala集合三大類(seq序列,set集,map映射)——map映射java

map映射:python

scala> import scala.collection.mutable.HashMap._c++

import scala.collection.mutable.HashMap._web

 

scala> val map1 = new HashMap[String,Int]()spa

map1: scala.collection.mutable.HashMap[String,Int] = HashMap()scala

 

scala> map1("scala") = 1rem

 

scala> map1it

res83: scala.collection.mutable.HashMap[String,Int] = HashMap(scala -> 1)io

 

scala> map1 += (("java",2))                 //使用+=添加一個元素table

res84: map1.type = HashMap(java -> 2, scala -> 1)

 

scala> map1

res85: scala.collection.mutable.HashMap[String,Int] = HashMap(java -> 2, scala -> 1)

 

scala> map1 += (("python",3),("web",4))    //添加多個元素

            ^

       warning: method += in trait Growable is deprecated (since 2.13.0): Use `++=` (addAll) instead of varargs `+=`

res86: map1.type = HashMap(python -> 3, java -> 2, web -> 4, scala -> 1)

 

scala> map1

res88: scala.collection.mutable.HashMap[String,Int] = HashMap(python -> 3, java -> 2, web -> 4, scala -> 1)

 

scala> map1 -= ("web")        //使用 -= 移除map裏面的一個元素

res89: map1.type = HashMap(python -> 3, java -> 2, scala -> 1)

 

scala> map1.remove("java")      //移除一個元素

res90: Option[Int] = Some(2)

 

scala> map1.put("c++",6)      //添加一個元素 put 操做方法實現

res92: Option[Int] = None

 

scala> map1

res93: scala.collection.mutable.HashMap[String,Int] = HashMap(python -> 3, c++ -> 6, scala -> 1)

相關文章
相關標籤/搜索