Set集合

来源:互联网 发布:淘宝店铺会员管理系统 编辑:程序博客网 时间:2024/06/13 23:16

Scala Set是相同类型成对的不同元素的集合。换句话说,一个集合是不包含重复元素的集合。 集合有两种:不可变(immutable)和可变(mutable)。可变对象和不可变对象之间的区别在于,当对象不可变时,对象本身无法更改。

默认情况下,Scala使用不可变的集合(Set)。 如果要使用可变集合(Set),则必须明确导入scala.collection.mutable.Set类。 如果要在同一集合中使用可变集合和不可变集合,则可以继续引用不可变集作为集合(Set),但可以将可变集合称为mutable.Set

// Empty set of integer typevar s : Set[Int] = Set()// Set of integer typevar s : Set[Int] = Set(1,3,5,7)// 或者var s = Set(1,3,5,7)
Scala

通过定义一个空集合,类型注释是必要的,因为系统需要将具体的类型分配给变量。

集合基本操作

所有对集合的操作都可以用以下三种方法来表示:

序号方法描述1head此方法返回列表的第一个元素。2tail此方法返回由除第一个之外的所有元素组成的列表。3isEmpty如果列表为空,则此方法返回true,否则返回false

以下示例显示如何使用上述方法。

示例

object Demo {   def main(args: Array[String]) {      val fruit = Set("apples", "oranges", "pears")      val nums: Set[Int] = Set()      println( "Head of fruit : " + fruit.head )      println( "Tail of fruit : " + fruit.tail )      println( "Check if fruit is empty : " + fruit.isEmpty )      println( "Check if nums is empty : " + nums.isEmpty )   }}
Scala

将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。

D:\>scalac Demo.scalaD:\>scala DemoHead of fruit : applesTail of fruit : Set(oranges, pears)Check if fruit is empty : falseCheck if nums is empty : true
Scala

连接集合

您可以使用++运算符或Set.++()方法连接两个或多个集合,但是在添加集合时,它将删除重复的元素。

以下是连接两个集合的例子 -

object Demo {   def main(args: Array[String]) {      val fruit1 = Set("apples", "oranges", "pears")      val fruit2 = Set("mangoes", "banana")      // use two or more sets with ++ as operator      var fruit = fruit1 ++ fruit2      println( "fruit1 ++ fruit2 : " + fruit )      // use two sets with ++ as method      fruit = fruit1.++(fruit2)      println( "fruit1.++(fruit2) : " + fruit )   }}
Scala

将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。

D:\>scalac Demo.scalaD:\>scala Demofruit1 ++ fruit2 : Set(banana, apples, mangoes, pears, oranges)fruit1.++(fruit2) : Set(banana, apples, mangoes, pears, oranges)
Scala

在集合中查找最大值,最小元素

可以使用Set.min方法和Set.max方法来分别找出集合中元素的最大值和最小值。 以下是显示程序的示例。

object Demo {   def main(args: Array[String]) {      val num = Set(5,6,9,20,30,45)      // find min and max of the elements      println( "Min element in Set(5,6,9,20,30,45) : " + num.min )      println( "Max element in Set(5,6,9,20,30,45) : " + num.max )   }}
Scala

将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。

D:\>scalac Demo.scalaD:\>scala DemoMin element in Set(5,6,9,20,30,45) : 5Max element in Set(5,6,9,20,30,45) : 45
Scala

查找交集值

可以使用Set.&Set.intersect方法来查找两个集合之间的交集(相交值)。尝试以下示例来显示用法。

object Demo {   def main(args: Array[String]) {      val num1 = Set(5,6,9,20,30,45)      val num2 = Set(50,60,9,20,35,55)      // find common elements between two sets      println( "num1.&(num2) : " + num1.&(num2) )      println( "num1.intersect(num2) : " + num1.intersect(num2) )   }}
Scala

将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。

D:\>scalac Demo.scalaD:\>scala Demonum1.&(num2) : Set(20, 9)num1.intersect(num2) : Set(20, 9)