Scala编程语言具有许多在Scala中的集合上运行的功能。在有关Scala的本教程中,我们学习了将在Scala中的集合上运行的函数。
集合是一种特殊类型的集合,其中包含所有唯一元素。在Scala中,许多功能可用于在Scala中操作数据。
让我们在Scala中查看其中一些功能,
在Scala中,某些功能可用于覆盖可变集合以执行加法运算。
+ =:此运算符用于将新元素添加到不可变集合中的可变集合中。
++ ==:此运算符用于将新元素添加到可变集合。
add():此方法用于将新元素添加到不可变集合中的可变集合中。
让我们举几个例子来了解代码的工作原理,
import scala.collection.mutable._ object Main { def main(args: Array[String]) { var myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400") println("My bike collection is : ") println(myBikes) print("Adding a new bike to my collection, ") myBikes += "Continental GT" println("My bike collection now is: ") println(myBikes) println("Adding some new bikes to my collection, ") myBikes ++== List("Bajaj Platina", "Splender") println("My bike collection now is: ") println(myBikes) print("Adding a new bike to my collection, ") myBikes.add("Hayabusa") println("My bike collection now is: ") println(myBikes) } }
输出结果
My bike collection is : HashSet(Thunderbird 350, Dominar 400, Ninja 300) Adding a new bike to my collection, My bike collection now is: HashSet(Thunderbird 350, Continental GT, Dominar 400, Ninja 300) Adding some new bikes to my collection, My bike collection now is: HashSet(Splender, Bajaj Platina, Dominar 400, Ninja 300, Thunderbird 350, Continental GT) Adding a new bike to my collection, My bike collection now is: HashSet(Splender, Hayabusa, Bajaj Platina, Dominar 400, Ninja 300, Thunderbird 350, Continental GT)
对于不可变的集合,我们不能将元素添加到现有集合中。但是我们可以添加具有不可变集合的元素,并将其存储到新变量中。对于这两个运算符可以使用,
+:此运算符用于将一个元素添加到集合中,然后将其存储到另一个变量中。
++:此运算符用于将多个元素添加到集合中,然后将该总和存储到另一个变量中。
示例
import scala.collection.immutable._ object Main { def main(args: Array[String]) { val myBikes = Set("R1" , "Thunderbird" , "GS390") val FBikes = Set("Bullet 350", "Continental GT" , "K1000") println("My Bike collection: ") println(myBikes) println("My friends Bike collection: ") println(FBikes) println("Adding one more bike to friends collection ") var FBikes2 = FBikes + "Ninja 300" println("My friends Bike collection now is: ") println(FBikes2) println("Adding more bikes to my collection ") var myBikes2 = myBikes ++ List("CBR 1000R" ,"R1100R" ) println("My friends Bike collection now is: ") println(myBikes2) println("Our collection togather has: ") var bike = myBikes2 ++ FBikes2 println(bike) } }
输出结果
My Bike collection: Set(R1, Thunderbird, GS390) My friends Bike collection: Set(Bullet 350, Continental GT, K1000) Adding one more bike to friends collection My friends Bike collection now is: Set(Bullet 350, Continental GT, K1000, Ninja 300) Adding more bikes to my collection My friends Bike collection now is: HashSet(R1100R, GS390, Thunderbird, R1, CBR 1000R) Our collection togather has: HashSet(R1100R, Ninja 300, GS390, K1000, Continental GT, Thunderbird, Bullet 350, R1, CBR 1000R)
在Scala编程语言中,从集合中删除元素是有效的。有可用于执行任务的运算符和方法。
-=:此运算符用于从集合中删除单个元素。
-=:此运算符用于从集合中删除多个元素。
方法retain()
,clear()
,remove()
用于删除可变集合从可变集合中的元素。
示例
import scala.collection.mutable._ object Main { def main(args: Array[String]) { var myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400", "Continental GT", "S1000RR") println("My bike collection is: ") println(myBikes) println("Removing dominar400 from my collection : ") myBikes -= "Dominar 400" println("My Collection is : ") println(myBikes) println("Removing few more bikes from my collection : ") myBikes --== List("S1000RR" , "Ninja 300") println("My Collection is : ") println(myBikes) } }
输出结果
My bike collection is: HashSet(Dominar 400, Ninja 300, Thunderbird 350, Continental GT, S1000RR) Removing dominar400 from my collection : My Collection is : HashSet(Ninja 300, Thunderbird 350, Continental GT, S1000RR) Removing few more bikes from my collection : My Collection is : HashSet(Thunderbird 350, Continental GT)
对于不可变集合,我们通常无法从集合中删除元素。我们需要将它们从集合中删除,然后将更新的集合存储到新的内存位置。运算符-和-对于从集合中删除元素有效。
示例
import scala.collection.immutable._ object Main { def main(args: Array[String]) { val myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400", "Continental GT", "S1000RR") println("My bike collection is : ") println(myBikes) println("Removing dominar400 from my collection: ") val myBikes2 = myBikes - "Dominar 400" println("My Collection is: ") println(myBikes2) println("Removing few more bikes from my collection: ") val myBikes3 = myBikes2 -- List("S1000RR" , "Ninja 300") println("My Collection is: ") println(myBikes3) } }
输出结果
My bike collection is : HashSet(Ninja 300, Continental GT, Dominar 400, S1000RR, Thunderbird 350) Removing dominar400 from my collection: My Collection is: HashSet(Ninja 300, Continental GT, S1000RR, Thunderbird 350) Removing few more bikes from my collection: My Collection is: HashSet(Continental GT, Thunderbird 350)