一、常用的集合方法
1.可遍历集合的常用方法
下表列出了Traverable在所有集合常用的方法。接下来的符号:
c代表一个集合
f代表一个函数
p代表一个谓词
n代表一个数字
op代表一个简单的操作(通常是一个简单的函数)
2.可变的集合方法
下表里的是可变集合常用的操作符:
3.不可变的集合方法
注意不可变集合不可被修改,所以在第一行的每个表达式的结果必须赋给一个新变量。
操作符 | 描述 |
c1 ++ c2 | 把c2集合的元素附加到c1里创造一个新的集合 |
scala> val c1 = Vector(1,2,3,4,5) c1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> val c2 = Vector(6,7,8) c2: scala.collection.immutable.Vector[Int] = Vector(6, 7, 8) scala> c1 ++ c2 res35: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8)
操作符 | 描述 |
c :+ e | 返回把元素e附加到集合c的新集合 |
scala> val v1 = Vector(1,2,3,4,5) c1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> val e = 10e: Int = 10scala> v1 :+ e res38: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5, 10)
操作符 | 描述 |
e +: c2 | 返回一个把元素e前插到集合c的新集合 |
scala> val v1 = Vector(1,2,3,4,5) c1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> val e = 10e: Int = 10scala> e +: v1 res39: scala.collection.immutable.Vector[Int] = Vector(10, 1, 2, 3, 4, 5)
操作符 | 描述 |
e :: list | 返回一个把e前插到名为list列表的集合(::只在List集合上用) |
scala> val list = List(7,8,9) list: List[Int] = List(7, 8, 9) scala> val e = 10e: Int = 10scala> e :: list res75: List[Int] = List(10, 7, 8, 9)
操作符 | 描述 |
c drop n | 把c2集合的元素附加到c1里创造一个新的集合 |
scala> val v1 = Vector(1,2,3,4,5) v1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> val n = 3n: Int = 3scala> v1 drop n res1: scala.collection.immutable.Vector[Int] = Vector(4, 5)
操作符 | 描述 |
c1 take n | 把集合的前两个元素列出来 |
scala> val c1 = Vector(1,2,3,4,5) c1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> c1 take 2res82: scala.collection.immutable.Vector[Int] = Vector(1, 2)
4.可变和不可变的map的常用方法
m代表一个不可变map
mm代表一个可变的map
k代表一个键
v代表一个值
p代表一个谓词
c代表一个集合
如果是var修饰,引用可变,支持读写。
如果是val修饰,引用不可变,支持读写。
操作符 | 描述 |
m - k | 返回删除key(以及相应的value的值)后的map |
scala> val v1 = Vector(1,2,3,4,5) v1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5) scala> val map = Map(1 -> "a",2 -> "b",3 -> "c") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c) scala> map - 1res5: scala.collection.immutable.Map[Int,String] = Map(2 -> b, 3 -> c)
操作符 | 描述 |
m - (k1,k2,k3) | 返回删除k1,k2,k3后的map |
scala> val map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d",5 -> "e",6 -> "f") map: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 1 -> a, 6 -> f, 2-> b, 3 -> c, 4 -> d) scala> map - (1,3,5) res6: scala.collection.immutable.Map[Int,String] = Map(6 -> f, 2 -> b, 4 -> d)
操作符 | 描述 |
m -- c | 返回删除key后的map,c是指集合 |
scala> val map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d",5 -> "e",6 -> "f") map: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 1 -> a, 6 -> f, 2-> b, 3 -> c, 4 -> d) scala> val c = Vector(1,3,5) c: scala.collection.immutable.Vector[Int] = Vector(1, 3, 5) scala> map -- c res7: scala.collection.immutable.Map[Int,String] = Map(6 -> f, 2 -> b, 4 -> d)
操作符 | 描述 |
m -- List(k1,k2) | 返回删除key后的map |
1scala> val map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d",5 -> "e",6 -> "f") map: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 1 -> a, 6 -> f, 2-> b, 3 -> c, 4 -> d) scala> val list = List(1,3,5) list: List[Int] = List(1, 3, 5) scala> map -- list res8: scala.collection.immutable.Map[Int,String] = Map(6 -> f, 2 -> b, 4 -> d)2scala> val map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d",5 -> "e",6 -> "f") map: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 1 -> a, 6 -> f, 2-> b, 3 -> c, 4 -> d) scala> map -- List(1,2,3) res9: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 6 -> f, 4 -> d)
操作符 | 描述 |
m(k) | 返回k的value |
scala> var map = Map(1 -> 1,2 -> 2,3 -> 3,4 -> 4) map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 ->4) scala> map(2) res34: Int = 2
操作符 | 描述 |
m contains k | 如果map包含k则返回true |
scala> var map = Map(1 -> 1,2 -> 2,3 -> 3,4 -> 4) map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3, 4 ->4) scala> map contains 3res36: Boolean = true
操作符 | 描述 |
m get k | 如果k存在返回Some[A]作为key的值,否则None |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map get 2res44: Option[String] = Some(b) scala> map get 5res45: Option[String] = None
操作符 | 描述 |
m getOrElse(k,d) | 如果k找到的话,返回k的值,否则返回默认值d |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map getOrElse(2,"hello") res46: String = b scala> map getOrElse(5,"hello") res47: String = hello
操作符 | 描述 |
m isDefinedAt k | 如果map包含k则返回true |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map isDefinedAt 2res48: Boolean = truescala> map isDefinedAt 5res49: Boolean = false
操作符 | 描述 |
m keys | 把map中的keys作为Iterable返回 |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map keys warning: there was one feature warning; re-run with -feature for details res50: Iterable[Int] = Set(1, 2, 3, 4)
操作符 | 描述 |
m keySet | 把map中的keys作为Set返回 |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map keySet warning: there was one feature warning; re-run with -feature for details res54: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
操作符 | 描述 |
m values | 把map中的value作为Iterable返回 |
scala> var map = Map(1 -> "a",2 -> "b",3 -> "c",4 -> "d") map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4-> d) scala> map values warning: there was one feature warning; re-run with -feature for details res56: Iterable[String] = MapLike(a, b, c, d)
二、在创建集合时声明一个类型
想要创建一个混合型的集合,scala不会自动分配想要的类型。
创建一个手动的集合如下:
scala> trait Animal defined trait Animal scala> trait FurryAnimal extends Animal defined trait FurryAnimal scala> case class Dog(name: String) extends Animal defined class Dog scala> case class Cat(name: String) extends Animal defined class Cat scala> val x = Array(Dog("Fido"),Cat("Felix")) x: Array[Product with Serializable with Animal] = Array(Dog(Fido), Cat(Felix))
如上所示,scala给product指定了Serializable和Animal类型。如果想要一个Array[Animal],手动需要指定类型:
scala> val x = Array[Animal](Dog("Fido"),Cat("Felix")) x: Array[Animal] = Array(Dog(Fido), Cat(Felix))
三、可变变量集合与不可变量集合
但把一个不可变的Vector赋给一个可变的var时,似乎可以给它添加元素。
scala> var sisters = Vector("Melinda") sisters: scala.collection.immutable.Vector[String] = Vector(Melinda) scala> sisters = sisters :+ "Melissa"sisters: scala.collection.immutable.Vector[String] = Vector(Melinda, Melissa) scala> sisters = sisters :+ "Marisa"sisters: scala.collection.immutable.Vector[String] = Vector(Melinda, Melissa, Ma risa) scala> sisters.foreach(println) Melinda Melissa Marisa
尽管你看起来改变了一个不可改变的集合,但实际上是每次用:+方法时sisters变量会指向一个新的集合。sisters变量是可变的,所以每次执行时相当于重新赋值一个新的集合。
总结:
一个可变的变量var可以被赋予一个新的数据。
一个不可变的变量val就如java中的final变量,不可能被重新赋值。
在可变集合的元素中可以被改变(ArrayBuffer)。
在不可被改变的集合元素中不可被改变(Vector)。
原文出处:https://www.cnblogs.com/zhaohadoopone/p/9517899.html
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦