您的位置:首页 > 其它

Scala集合操作—Set

2016-10-13 23:45 323 查看
Scala集合Set常用操作:点击打开链接

Scala集合为相同类型的配对的不同元素的集合。换句话说,集合是不包含重复元素的集合。有两种集合,不可改变的和可变的。可变和不可变的对象之间的区别在于,当一个对象是不可变的,对象本身不能被改变。

默认情况下,Scala中使用不可变的集。如果想使用可变集,必须明确地导入scala.collection.mutable.Set类。如果想在同一个同时使用可变和不可变的集合,那么可以继续参考不变的集合,但可以参考可变设为mutable.Set。以下是声明不变集合示例:

<span style="font-family:Times New Roman;"><span style="font-family:Times New Roman;font-size:14px;">   </span>
<span style="font-family:Times New Roman;">    var s:Set[Int]=Set()
// Set of integer type
var s1:Set[Int]=Set(1,3,5,7)
var s2=Set(1,3,5,7)</span></span>
在定义空集,类型注释是必要的,因为系统需要指定一个具体的类型变量。

集合基本操作:

集合所有操作可以体现在以下三个方法:
<span style="font-family:Times New Roman;"><span style="font-family:Times New Roman;font-size:14px;">      val fruit = Set("apples", "oranges", "pears")
val nums: Set[Int] = Set()

println( "Head of fruit : " + fruit.head )//head:返回集合的首元素
println( "Tail of fruit : " + fruit.tail )//tail:该方法返回集合由除第一个以外的所有元素。
println( "Check if fruit is empty : " + fruit.isEmpty )//如果集合为空,此方法返回true,否则为false。
println( "Check if nums is empty : " + nums.isEmpty )</span></span>
运行结果:
 
<span style="font-family:Times New Roman;">Head of fruit : apples
Tail of fruit : Set(oranges, pears)
Check if fruit is empty : false
Check if nums is empty : true</span></span>





串联集合:

可以使用++运算符或集。++()方法来连接两个或多个集,但同时增加了集它会删除重复的元素。以下是这个例子来连接两个集合val        fruit1 = Set("apples",
"oranges", "pears")

<span style="font-family:Times New Roman;"><span style="font-family:Times New Roman;font-size:14px;">      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 )</span></span>
运行结果:

<span style="font-family:Times New Roman;"> fruit1 ++ fruit2 : Set(banana, apples, mangoes, pears, oranges)
fruit1.++(fruit2) : Set(banana, apples, mangoes, pears, oranges)
</span>




查找集合中最大,最小的元素和集合的共同值:


     1).可以使用Set.min方法找出最小元素,Set.max方法找出一组可用最大元素。


      2).使用Set.&方法或Set.intersect方法找出两个集合之间的共同值。


      以下为例子来说明用法:

<span style="font-size: 14px;">  </span><span style="font-family:Times New Roman;font-size:12px;"> <span style="font-weight: normal;">val num1 = Set(5,6,9,20,30,45)
val num2 = Set(50,60,9,20,35,55)

println( "Min element in Set(5,6,9,20,30,45) : " + num1.min )
println( "Max element in Set(5,6,9,20,30,45) : " + num1.max )
// find common elements between two sets
println( "num1.&(num2) : " + num1.&(num2) )
println( "num1.intersect(num2) : " + num1.intersect(num2) )</span></span>


 运行结果:


<span style="font-size:12px;"><span style="font-family: Arial, Helvetica, sans-serif; line-height: 1.5em;">   </span><span style="font-family:Times New Roman;"><span style="line-height: 1.5em;"> <span style="font-weight: normal;">   </span></span><span style="line-height: 1.5em; font-weight: normal;">Min element in Set(5,6,9,20,30,45) : 5</span></span></span>
<span style="font-family:Times New Roman;font-size:12px;font-weight: normal;">        Max element in Set(5,6,9,20,30,45) : 45
num1.&(num2) : Set(20, 9)
num1.intersect(num2) : Set(20, 9)</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Scala Set