scala的多种集合的使用之映射Map的操作方法
Posted zhaohadoopone
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala的多种集合的使用之映射Map的操作方法相关的知识,希望对你有一定的参考价值。
1.创建映射
1)创建不可变的映射
scala> val status = Map(1 -> "a",2 -> "b") status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b) scala> val status = Map((1,"a"),(2,"b")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)
2)创建可变的映射,要么用导入的方式将其带入作用域中,要么就在创建实例时指定scala.collection.mutable.Map类的完整路径。
scala> var status = collection.mutable.Map((1,"a"),(2,"b")) status: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a)
3)在创建的时候创建一空可变的Map,之后在添加元素。
scala> var status = collection.mutable.Map[Int,String]() status: scala.collection.mutable.Map[Int,String] = Map() scala> status += ((1,"a")) res38: scala.collection.mutable.Map[Int,String] = Map(1 -> a) scala> status += ((3,"c"),(2,"b")) res39: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
2.Map使用的场景
1)想要返回元素按照键有序的映射,请使用SortedMap。
scala> import scala.collection.SortedMap import scala.collection.SortedMap scala> val grades = SortedMap(("kim",90),("al",86),("mes",88),("ema",78),("han",93)) grades: scala.collection.SortedMap[String,Int] = Map(al -> 86, ema -> 78, han ->93, kim -> 90, mes -> 88) scala> val grades = SortedMap((1,90),(3,86),(2,88),(5,78),(4,93)) grades: scala.collection.SortedMap[Int,Int] = Map(1 -> 90, 2 -> 88, 3 -> 86, 4 -> 93, 5 -> 78)
2)按插入时的顺序返回元素,只有可变的LinkedHashMap。
scala> import scala.collection.mutable.LinkedHashMap import scala.collection.mutable.LinkedHashMap scala> var status = LinkedHashMap((5,"apple")) status: scala.collection.mutable.LinkedHashMap[Int,String] = Map(5 -> apple) scala> status += ((3,"orange")) res40: scala.collection.mutable.LinkedHashMap[Int,String] = Map(5 -> apple, 3 -> orange) scala> status += ((6,"banana")) res41: scala.collection.mutable.LinkedHashMap[Int,String] = Map(5 -> apple, 3 ->orange, 6 -> banana)
3)按插入时的相反顺序返回元素,可以是可变的或者不可变的ListMap。
scala> import scala.collection.mutable.ListMap import scala.collection.mutable.ListMap scala> var status = ListMap((1,"a")) status: scala.collection.mutable.ListMap[Int,String] = Map(1 -> a) scala> status += ((1,"a")) res43: scala.collection.mutable.ListMap[Int,String] = Map(1 -> a) scala> status += ((2,"b")) res44: scala.collection.mutable.ListMap[Int,String] = Map(2 -> b, 1 -> a) scala> status += ((3,"c")) res45: scala.collection.mutable.ListMap[Int,String] = Map(3 -> c, 1 -> a, 2 -> b)
3.可变映射的添加、更新和删除元素
1)通过给键指定值的方式为可变映射添加元素。
scala> var status = scala.collection.mutable.Map[String,String]() status: scala.collection.mutable.Map[String,String] = Map() scala> status("a1") = "a1a" scala> status res47: scala.collection.mutable.Map[String,String] = Map(a1 -> a1a)
2)通过+=方法添加一个或者多个元素。
scala> var status = scala.collection.mutable.Map[String,String]() status: scala.collection.mutable.Map[String,String] = Map() scala> status += (("a1","a1a")) res50: status.type = Map(a1 -> a1a) scala> status res51: scala.collection.mutable.Map[String,String] = Map(a1 -> a1a) scala> status += (("a1","a1a"),("a2","a2a")) res52: status.type = Map(a1 -> a1a, a2 -> a2a) scala> status res53: scala.collection.mutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a)
3)用++=从另一个集合添加多个元素。
scala> var status = scala.collection.mutable.Map[String,String]() status: scala.collection.mutable.Map[String,String] = Map() scala> status ++= List(("a1","a1a"),("a2","a2a")) res55: status.type = Map(a1 -> a1a, a2 -> a2a)
4)用-=的方法通过指定元素的键从映射中删除一个或者多个元素。
scala> status ++= List(("a1","a1a"),("a2","a2a")) res56: status.type = Map(a1 -> a1a, a2 -> a2a) scala> status -= "a1" res57: status.type = Map(a2 -> a2a) scala> status res58: scala.collection.mutable.Map[String,String] = Map(a2 -> a2a) scala> status -= ("a1","a2") res60: status.type = Map() scala> status res61: scala.collection.mutable.Map[String,String] = Map()
5)用--=删除集合里的指定的元素。
scala> var status = scala.collection.mutable.Map[String,String]() status: scala.collection.mutable.Map[String,String] = Map() scala> status ++= List(("a1","a1a"),("a2","a2a")) res67: status.type = Map(a1 -> a1a, a2 -> a2a) scala> status --= List("a1","a2") res68: status.type = Map() scala> status res69: scala.collection.mutable.Map[String,String] = Map()
6)通过赋值值给元素的键更新元素。
scala> var status = scala.collection.mutable.Map[String,String]() status: scala.collection.mutable.Map[String,String] = Map() scala> status ++= List(("a1","a1a"),("a2","a2a")) res72: status.type = Map(a1 -> a1a, a2 -> a2a) scala> status("a1") = "hello world" scala> status res74: scala.collection.mutable.Map[String,String] = Map(a1 -> hello world, a2 -> a2a)
4.不可变映射的添加、更新和删除元素
1)用+个方法添加一个或者多个元素,在这个过程中将结果赋给一个新的变量。
scala> val a = Map(("a1","a1a")) a: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a) scala> val b = a + (("a2","a2a")) b: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a) scala> val c = b + (("a3","a31"),("a4","a4a")) c: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a31, a4 -> a4a)
2)更新一个不可变映射的键值对,需要用+方法对键/值重新赋值,新值替换旧值。
scala> val a = Map(("a1","a1a")) a: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a) scala> val b = a + (("a2","a2a")) b: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a) scala> val c = b + (("a1","hello world")) c: scala.collection.immutable.Map[String,String] = Map(a1 -> hello world, a2 -> a2a)
3)使用-方法删除一个或者多个元素。
scala> val a = Map(("a1","a1a")) a: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a) scala> val b = a + (("a2","a2a"),("a3","a3a"),("a4","a4a")) b: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a3a, a4 -> a4a) scala> val c = b - "a1" - "a2" c: scala.collection.immutable.Map[String,String] = Map(a3 -> a3a, a4 -> a4a) scala> val d = c - "a4" d: scala.collection.immutable.Map[String,String] = Map(a3 -> a3a)
当一个不可变的变量声明为var时,它仍然是一个不可变的映射,不能给映射中的元素重新赋值。
5.映射值的访问
1)访问保存在映射中单独的值,如果键不存在,会抛出异常。为了避免这个问题,可以在创建映射时使用withDefaultValue的方法。该方法会创建一个默认值,如果键没有找到,映射会返回这个值。
scala> val status = Map((1,"a"),(2,"b"),(3,"c")).withDefaultValue("Not Found") status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c) scala> status(4) res5: String = Not Found scala> status(3) res6: String = c
2)寻找键时可以使用getOrElse方法,当指定的键找不到时,会返回默认值。
scala> val status = Map((1,"a"),(2,"b"),(3,"c")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c) scala> val s = status.getOrElse(6,"Not such value") s: String = Not such value
3)可以使用get方法返回Option对象。
scala> val status = Map((1,"a"),(2,"b"),(3,"c")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c) scala> val s = status.get(5) s: Option[String] = None scala> val s = status.get(2) s: Option[String] = Some(b)
6.映射的遍历
1)for循环遍历所有的映射元素。
scala> val status = Map((1,"a"),(2,"b"),(3,"c"),(4,"d")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d) scala> for((k,v) <- status) println(s"key: $k, value: $v") key: 1, value: a key: 2, value: b key: 3, value: c key: 4, value: d
2)匹配表达式配合foreach方法。
scala> val status = Map((1,"a"),(2,"b"),(3,"c"),(4,"d")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d) scala> status.foreach{ | case(k,v) => println(s"key: $k,value: $v") | } key: 1,value: a key: 2,value: b key: 3,value: c key: 4,value: d
3)使用Tuple语法访问键/值字段。
scala> val status = Map((1,"a"),(2,"b"),(3,"c"),(4,"d")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d) scala> status.foreach(x => println(s"key: ${x._1},value: ${x._2}")) key: 1,value: a key: 2,value: b key: 3,value: c key: 4,value: d
4)如果想要映射中所有的键,keys方法返回Iterable。
scala> val status = Map((1,"a"),(2,"b"),(3,"c"),(4,"d")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d) scala> status.keys.foreach((key) => println(key)) 1 2 3 4
5)如果想要映射中所有的value的值,用values方法可以遍历映射中所有的值。
scala> val status = Map((1,"a"),(2,"b"),(3,"c"),(4,"d")) status: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d) scala> status.values.foreach((value) => println(value)) a b c d
7.操作映射的值
1)如果要遍历映射并对每个值进行操作,mapValues是个不错的选择。它可以在每个映射值上执行一个函数,然后返回修改后的映射。
scala> var x = collection.mutable.Map((1,"a"),(2,"b")) x: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a) scala> val y = x.mapValues(_.toUpperCase) y: scala.collection.Map[Int,String] = Map(2 -> B, 1 -> A)
2)transform方法可以同时使用键/值实现一个变换方法。
scala> val map = Map((1,10),(2,20),(3,30)) map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20, 3 -> 30) scala> val newMap = map.transform((k,v) => k + v) newMap: scala.collection.immutable.Map[Int,Int] = Map(1 -> 11, 2 -> 22, 3 -> 33)
8.从映射中获取所有的键和值
1)用keySet的方法将以集合的方式获取所有的键。
scala> val status = Map(("a1","a1a"),("a2","a2a"),("a3","a3a")) status: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a3a) scala> status.keySet res14: scala.collection.immutable.Set[String] = Set(a1, a2, a3)
2)用keys方法获得一个Iterable。
scala> status.keys res16: Iterable[String] = Set(a1, a2, a3)
3)用keysIterator方法获取所有作为迭代器的键。
scala> status.keysIterator res17: Iterator[String] = non-empty iterator
4)使用values方法可以获取映射中所有的值,并将结果转化为Iterable对象。
scala> status.values res18: Iterable[String] = MapLike(a1a, a2a, a3a)
5)使用valuesIterator方法,返回Iterator对象。
scala> status.valuesIterator res19: Iterator[String] = non-empty iterator
keysIterator和valuesIterator方法都会从映射数据返回一个迭代器。这些方法不会创建一个新的集合,仅仅是提供遍历已存在的迭代器。
9.反转键值
可以用for推导反转映射的键值,然后将结果赋给一个新的变量。但是映射中,key是不重复的,value是可以重复的,但是在反转时可能会丢掉数据。
scala> val status = Map(("a1","a1a"),("a2","a2a"),("a3","a3a")) status: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a3a) scala> val newMap = for((k,v) <- status) yield (v,k) newMap: scala.collection.immutable.Map[String,String] = Map(a1a -> a1, a2a -> a2, a3a -> a3)
10.测试映射中键/值的存在
1)用contains方法测试映射中是否包含键。
scala> val status = Map(("a1","a1a"),("a2","a2a"),("a3","a3a")) status: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a3a) scala> if (status.contains("a1")) println("found a1") else println("not found") found a1 scala> if (status.contains("a5")) println("found a5") else println("not found") not found
2)使用valuesIterator方法搜索值,结合exists和contains。
scala> val status = Map(("a1","a1a"),("a2","a2a"),("a3","a3a")) status: scala.collection.immutable.Map[String,String] = Map(a1 -> a1a, a2 -> a2a, a3 -> a3a) scala> status.valuesIterator.exists(_.contains("a2a")) res22: Boolean = true scala> status.valuesIterator.exists(_.contains("a2a2")) res23: Boolean = false
11.根据键或者值对映射排序
1)可以用sortBy方法对值(_2)/键(_1)从低到高进对映射进行排序。
scala> val grade = Map((1,98),(2,89),(3,88),(4,93),(5,95)) grade: scala.collection.immutable.Map[Int,Int] = Map(5 -> 95, 1 -> 98, 2 -> 89, 3 -> 88, 4 -> 93) scala> import scala.collection.immutable.ListMap import scala.collection.immutable.ListMap scala> ListMap(grade.toSeq.sortBy(_._2):_*) res25: scala.collection.immutable.ListMap[Int,Int] = Map(3 -> 88, 2 -> 89, 4 -> 93, 5 -> 95, 1 -> 98)
2)可以用sortWith方法对值(_2)/键(_1)按照升序或者降序对值进行排序。
//按照值升序: scala> ListMap(grade.toSeq.sortWith(_._2 < _._2):_*) res28: scala.collection.immutable.ListMap[Int,Int] = Map(3 -> 88, 2 -> 89, 4 ->93, 5 -> 95, 1 -> 98) //按照值降序: scala> ListMap(grade.toSeq.sortWith(_._2 > _._2):_*) res29: scala.collection.immutable.ListMap[Int,Int] = Map(1 -> 98, 5 -> 95, 4 ->93, 2 -> 89, 3 -> 88)
关于_*:
它的作用是将数据转换,然后将其作为多个参数传给ListMap。
12.映射中键值的最值
1)对键进行排序。
scala> val grade = Map((1,98),(2,89),(3,88),(4,93),(5,95)) grade: scala.collection.immutable.Map[Int,Int] = Map(5 -> 95, 1 -> 98, 2 -3 -> 88, 4 -> 93) scala> grade.max res31: (Int, Int) = (5,95) scala> grade.keysIterator.max res32: Int = 5 scala> grade.keysIterator.reduceLeft((x,y) => if(x > y) x else y) res33: Int = 5
2)对值进行排序 。
scala> val grade = Map((1,98),(2,89),(3,88),(4,93),(5,95)) grade: scala.collection.immutable.Map[Int,Int] = Map(5 -> 95, 1 -> 98, 2 -> 89,3 -> 88, 4 -> 93) scala> grade.valuesIterator.max res40: Int = 98 scala> grade.valuesIterator.reduceLeft((x,y) => if(x > y) x else y) res41: Int = 98 scala> grade.valuesIterator.reduceLeft(_ max _) res42: Int = 98
以上是关于scala的多种集合的使用之映射Map的操作方法的主要内容,如果未能解决你的问题,请参考以下文章