Set集合

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

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

// Empty set of integer type
var s : Set[Int] = Set()

// Set of integer type
var 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.scala
D:\>scala Demo

Head of fruit : apples
Tail of fruit : Set(oranges, pears)
Check if fruit is empty : false
Check 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.scala
D:\>scala Demo

fruit1 ++ 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.scala
D:\>scala Demo

Min element in Set(5,6,9,20,30,45) : 5
Max 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.scala
D:\>scala Demo

num1.&(num2) : Set(20, 9)
num1.intersect(num2) : Set(20, 9)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值