Set 特性
不重复、无序
不可变set
创建Set&
- apply方式创建
val set=Set[Int](1,2,3,4,5)
查看setApi
进入 scala
$ scala
Welcome to Scala 2.12.10 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172).
Type in expressions for evaluation. Or try :help.
scala> val set=Set[Int](1,2,3,4,5)
set: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
set. <tab>
& exists min tails
&~ filter minBy take
+ filterNot mkString takeRight
++ find nonEmpty takeWhile
++: flatMap par to
- flatten partition toArray
-- fold product toBuffer
/: foldLeft reduce toIndexedSeq
:\ foldRight reduceLeft toIterable
WithFilter forall reduceLeftOption toIterator
addString foreach reduceOption toList
aggregate genericBuilder reduceRight toMap
andThen groupBy reduceRightOption toSeq
apply grouped repr toSet
canEqual hasDefiniteSize sameElements toStream
collect hashCode scan toString
collectFirst head scanLeft toTraversable
companion headOption scanRight toVector
compose init seq transpose
contains inits size union
copyToArray intersect slice unzip
copyToBuffer isEmpty sliding unzip3
count isTraversableAgain span view
diff iterator splitAt withFilter
drop last stringPrefix zip
dropRight lastOption subsetOf zipAll
dropWhile map subsets zipWithIndex
empty max sum |
equals maxBy tail
初始化集合
val set1=Set[Int](1,2,3,4,5)
val set2=Set[Int](4,5,6,7,8)
- 交集
&
方式
val newSet=set1.&(set2)
println(newSet)
Set(5, 4)
intersect
方式
val newSet=set1.intersect(set2)
println(newSet)
Set(5, 4)
- 返回两个集合的差集
&~
val newSet=set1.&~(set2)
println(newSet)
Set(1, 2, 3)
diff
val newSet=set1.diff(set2)
println(newSet)
Set(1, 2, 3)
- 添加元素
+
添加单个元素
val newSet=set1.+(10)
println(newSet)
Set(5, 10, 1, 2, 3, 4)
- 合并两个集合
++
val newSet=set1.++(set2)
println(newSet)
Set(5, 1, 6, 2, 7, 3, 8, 4)
++:
val newSet=set1.++:(set2)
println(newSet)
Set(5, 1, 6, 2, 7, 3, 8, 4)
- 删除元素
-
删除单个元素
val newSet=set1.-(2)
println(newSet)
Set(5, 1, 3, 4)
--
删除一组元素
val newSet=set1.--(Set[Int](1,2))
println(newSet)
Set(5, 3, 4)
- 遍历set
for(e <- set1){
println(s"e=$e")
}
e=5
e=1
e=2
e=3
e=4
可变set
创建可变set
导包
import scala.collection.mutable
采用apply方式创建
val set: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
查看可变set支持的api
scala> set.
& dropRight maxBy sum
&~ dropWhile min tail
+ empty minBy tails
++ equals mkString take
++: exists nonEmpty takeRight
++= filter par takeWhile
+= filterNot partition to
- find product toArray
-- flatMap reduce toBuffer
--= flatten reduceLeft toIndexedSeq
-= fold reduceLeftOption toIterable
/: foldLeft reduceOption toIterator
:\ foldRight reduceRight toList
<< forall reduceRightOption toMap
WithFilter foreach remove toSeq
add genericBuilder repr toSet
addString groupBy result toStream
aggregate grouped retain toString
andThen hasDefiniteSize sameElements toTraversable
apply hashCode scan toVector
canEqual head scanLeft transpose
clear headOption scanRight union
clone init seq unzip
collect inits size unzip3
collectFirst intersect sizeHint update
companion isEmpty sizeHintBounded view
compose isTraversableAgain slice withFilter
contains iterator sliding zip
copyToArray last span zipAll
copyToBuffer lastOption splitAt zipWithIndex
count map stringPrefix |
diff mapResult subsetOf
drop max subsets
&
交集
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](4,5,6,7,8,9,10)
println(set1.&(set2))
Set(5, 6, 7, 4)
&~
差集
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](4,5,6,7,8,9,10)
set1
println(set1.&~(set2))
Set(1, 2, 3)
set2
println(set2.&~(set1))
Set(9, 10, 8)
- 添加元素
+
添加单个元素并返回新的set
val set: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val newSet: mutable.Set[Int] = set.+(8)
println(newSet)
Set(1, 5, 2, 6, 3, 7, 4, 8)
++
和 ++:
添加一组元素并返回新的set
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](5,6,7,8,9,10)
val newSet: mutable.Set[Int] = set1.++(set2)
println(newSet)
Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8) //自动去重
++=
在原set列表中添加一组元素
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](5,6,7,8,9,10)
set1.++=(set2)
println(set1)
Set(9, 1, 5, 2, 6, 3, 10, 7, 4, 8) // 自动去重
+=
在原set列表中添加单个元素
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
set1.+=(9)
println(set1)
Set(9, 1, 5, 2, 6, 3, 7, 4)
- 删除元素
-
删除单个元素,并返回一个新的set,原set不受影响
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val newSet: mutable.Set[Int] = set1.-(3)
println(newSet)
Set(1, 5, 2, 6, 7, 4)
--
删除一组元素,并返回一个新的set,原set不受影响
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](5,6,7,8,9,10)
val newSet: mutable.Set[Int] = set1.--(set2)
println(newSet)
Set(1, 2, 3, 4)
--=
在原set列表中删除一组元素
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
val set2: mutable.Set[Int] = mutable.Set[Int](5,6,7,8,9,10)
set1.--=(set2)
println(set1)
Set(1, 2, 3, 4)
-=
在原set列表中删除一个元素
val set1: mutable.Set[Int] = mutable.Set[Int](1, 2, 3, 4, 5, 6, 7)
set1.-=(4)
println(set1)
Set(1, 5, 2, 6, 3, 7)
网友评论