集合类之Map(十二)

版权声明:菲立思教育 https://blog.csdn.net/cold_wolfie/article/details/82534048

简介

  Map(映射)也叫哈希表(Hash tables),是一种可迭代的键值对(key/value)结构。所有的值都可以通过键来获取。Map中的键都是唯一的。

  Map有两种类型:可变与不可变。区别在于可变对象可以修改,而不可变对象不可以。默认情况下 Scala 使用不可变 Map。如果使用可变集合,需要显式地引入 import scala.collection.mutable.Map 类。

简单实例

object Test {
   def main(args: Array[String]) {
      val colors = Map("red" -> "#FF0000",
                       "azure" -> "#F0FFFF",
                       "peru" -> "#CD853F")

      val nums: Map[Int, Int] = Map()

      println( "colors 中的键为 : " + colors.keys )
      println( "colors 中的值为 : " + colors.values )
      println( "检测 colors 是否为空 : " + colors.isEmpty )
      println( "检测 nums 是否为空 : " + nums.isEmpty )
   }
}
object Test {
   def main(args: Array[String]) {
      val colors1 = Map("red" -> "#FF0000",
                        "azure" -> "#F0FFFF",
                        "peru" -> "#CD853F")
      val colors2 = Map("blue" -> "#0033FF",
                        "yellow" -> "#FFFF00",
                        "red" -> "#FF0000")

      //  ++ 作为运算符
      var colors = colors1 ++ colors2
      println( "colors1 ++ colors2 : " + colors )

      //  ++ 作为方法
      colors = colors1.++(colors2)
      println( "colors1.++(colors2)) : " + colors )

   }
}

常见操作

//直接初始化
// ->操作符,左边是key,右边是value
scala> val studentInfo=Map("john" -> 21, "stephen" -> 22,"lucy" -> 20)
studentInfo: scala.collection.immutable.Map[String,Int] = Map(john -> 21, stephe
n -> 22, lucy -> 20)

//immutable不可变,它不具有以下操作
scala> studentInfo.clear()
<console>:10: error: value clear is not a member of scala.collection.immutable.M
ap[String,Int]
              studentInfo.clear()
                          ^
//创建可变的Map
scala> val studentInfoMutable=scala.collection.mutable.Map("john" -> 21, "stephe
n" -> 22,"lucy" -> 20)
studentInfoMutable: scala.collection.mutable.Map[String,Int] = Map(john -> 21, l
ucy -> 20, stephen -> 22)
//mutable Map可变,比如可以将其内容清空
scala> studentInfoMutable.clear()

scala> studentInfoMutable
res3: scala.collection.mutable.Map[String,Int] = Map()

//遍历操作1
scala> for( i <- studentInfoMutable ) println(i)
(john,21)
(lucy,20)
(stephen,22)

//遍历操作2
scala> studentInfoMutable.foreach(e=>
{val (k,v)=e; println(k+":"+v)}
)
john:21
lucy:20
stephen:22
//遍历操作3
scala> studentInfoMutable.foreach(e=> println(e._1+":"+e._2))
john:21
lucy:20
stephen:22

//定义一个空的Map
scala> val xMap=new scala.collection.mutable.HashMap[String,Int]()
xMap: scala.collection.mutable.HashMap[String,Int] = Map()

//往里面填充值
scala> xMap.put("spark",1)
res12: Option[Int] = None

scala> xMap
res13: scala.collection.mutable.HashMap[String,Int] = Map(spark -> 1)

//判断是否包含spark字符串
scala> xMap.contains("spark")
res14: Boolean = true

//-> 初始化Map,也可以通过 ("spark",1)这种方式实现(元组的形式)
scala> val xMap=scala.collection.mutable.Map(("spark",1),("hive",1))
xMap: scala.collection.mutable.Map[String,Int] = Map(spark -> 1, hive -> 1)

scala> "spark" -> 1
res18: (String, Int) = (spark,1)

//获取元素
scala> xMap.get("spark")
res19: Option[Int] = Some(1)

scala> xMap.get("SparkSQL")
res20: Option[Int] = None

忠于技术,热爱分享。欢迎关注公众号:java大数据编程,了解更多技术内容。

这里写图片描述

猜你喜欢

转载自blog.csdn.net/cold_wolfie/article/details/82534048
今日推荐