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)
通过定义一个空集合,类型注释是必要的,因为系统需要将具体的类型分配给变量。
集合基本操作
所有对集合的操作都可以用以下三种方法来表示:
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 ) }}
将上述程序保存在源文件: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
连接集合
您可以使用++
运算符或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 ) }}
将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。
D:\>scalac Demo.scalaD:\>scala Demofruit1 ++ fruit2 : Set(banana, apples, mangoes, pears, oranges)fruit1.++(fruit2) : Set(banana, apples, mangoes, pears, oranges)
在集合中查找最大值,最小元素
可以使用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 ) }}
将上述程序保存在源文件: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
查找交集值
可以使用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) ) }}
将上述程序保存在源文件:Demo.scala中,使用以下命令编译和执行此程序。
D:\>scalac Demo.scalaD:\>scala Demonum1.&(num2) : Set(20, 9)num1.intersect(num2) : Set(20, 9)
阅读全文
0 0
- Set 集合
- Set集合
- Set集合
- Set集合
- set集合
- Set集合
- Set集合
- Set集合
- Set 集合
- 集合set
- Set集合
- Set集合
- Set集合
- Set集合
- 集合set
- 集合Set
- Set集合
- 集合Set
- 配置snooping和ipsg遇到的特定IP的情况处理
- 职责链模式(chain of responsibility)c++版本
- python 报错解决办法
- 实现日期任意跳转
- 【转】libscws(简体中文分词库)
- Set集合
- Android 里的数据储存
- 打开r40平台tinav2.1下的以太网卡(分色排版)
- spring session解决集群环境下session共享问题
- 直方图均衡化
- 前端-JS基于原型面向对象(对象创建,继承,构造等)
- request和response笔记
- Qt笔记-qml-radionButton
- python判断网络是否通