scala 函数中嵌套函数_Scala Set函数与示例

scala 函数中嵌套函数

Scala设置函数 (Scala Set Functions)

Scala programming language has many functions that work on sets in Scala. In this tutorial on Scala, we learn about functions that will operate on sets in Scala.

Scala编程语言具有许多在Scala中处理集合的功能。 在有关Scala的本教程中,我们了解将在Scala中的集合上运行的函数。

A set is a special type of collection which contains all unique elements. In Scala many functions are available to for manipulating data in Scala.

集合是一种特殊类型的集合,其中包含所有唯一元素。 在Scala中,许多功能可用于在Scala中处理数据。

Let's see some of these functions in Scala,

让我们在Scala中查看其中一些功能,

1)在集合中添加元素(在可变集合上) (1) Adding elements in set (on mutable set))

In Scala, some functions can be used to over mutable sets to perform addition operation.

在Scala中,某些功能可用于覆盖可变集合以执行加法运算。

  • +=: This operator is used to add new elements to a mutable set in the immutable collection.

    + = :此运算符用于将新元素添加到不可变集合中的可变集合中。

  • ++==: This operator is used to add new elements to a mutable set.

    ++ == :此运算符用于将新元素添加到可变集合。

  • add(): This method is used to add new elements to a mutable set in an immutable collection.

    add() :此方法用于将新元素添加到不可变集合中的可变集合中。

Let's take a few examples to understand the working of the code,

让我们举几个例子来了解代码的工作原理,

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) 
	} 
} 

Output

输出量

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)

2)在集合中添加元素(在不可变元素上) (2) Adding elements in set (On immutable elements))

In the case of an immutable set, we cannot add elements to the existing set. But we can add elements with an immutable set and store this into a new variable. For these two operators can be used,

对于不可变的集合,我们不能将元素添加到现有集合中。 但是我们可以添加具有不可变集合的元素,并将其存储到新变量中。 对于这两个运算符可以使用,

  • +: This operator is used to add one element to the set and then store it into another variable.

    + :此运算符用于将一个元素添加到集合中,然后将其存储到另一个变量中。

  • ++: This operator is used to add multiple elements to the set and then store this sum into another variable.

    ++ :此运算符用于将多个元素添加到集合中,然后将该总和存储到另一个变量中。

Example:

例:

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)
	} 
} 

Output

输出量

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)

3)从集合中删除元素(在可变集合上) (3) Removing elements from sets (on mutable set))

In Scala programming language removing elements from a set is valid. There are operators as well as methods that can be used to perform the task.

在Scala编程语言中,从集合中删除元素是有效的。 有可用于执行任务的运算符和方法。

  • -=: this operator is used to delete a single element from the set.

    -= :此运算符用于从集合中删除单个元素。

  • --=: this operator is used to delete multiple elements from the set.

    -= :此运算符用于从集合中删除多个元素。

Methods retain(), clear(), remove() are used to delete elements from mutable set in mutable collection.

方法retain() , clear() , remove()用于从可变集合中的可变集中删除元素。

Example:

例:

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)
	}  
}

Output

输出量

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)

4)从集合中删除元素(在不可变集合上) (4) Removing elements from sets (on immutable set))

In the case of immutable sets, we cannot normally remove the elements from the set. We need to remove them from a set and then store the updated set to a new memory location. Operators - and -- are valid for removing elements from the set.

对于不可变集合,我们通常不能从集合中删除元素。 我们需要从集合中删除它们,然后将更新的集合存储到新的内存位置。 运算符--对于从集合中删除元素有效。

Example:

例:

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)
	}  
}

Output

输出量

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)


翻译自: https://www.includehelp.com/scala/set-functions-with-examples.aspx

scala 函数中嵌套函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值