Scala集合操作—Set
2016-10-13 23:45
323 查看
Scala集合Set常用操作:点击打开链接
Scala集合为相同类型的配对的不同元素的集合。换句话说,集合是不包含重复元素的集合。有两种集合,不可改变的和可变的。可变和不可变的对象之间的区别在于,当一个对象是不可变的,对象本身不能被改变。
默认情况下,Scala中使用不可变的集。如果想使用可变集,必须明确地导入scala.collection.mutable.Set类。如果想在同一个同时使用可变和不可变的集合,那么可以继续参考不变的集合,但可以参考可变设为mutable.Set。以下是声明不变集合示例:
集合基本操作:
集合所有操作可以体现在以下三个方法:
可以使用++运算符或集。++()方法来连接两个或多个集,但同时增加了集它会删除重复的元素。以下是这个例子来连接两个集合val fruit1 = Set("apples",
"oranges", "pears")
运行结果:
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>
相关文章推荐
- 3.python的set集合的操作示例——《深入python》学习
- Java集合工具类(一)-泛型集合工具类,用于便捷快速的定义、操作集合。包含Set的交集、并集...
- Python集合(set)类型的操作
- Scala中的集合:Iterator、BitSet、Set、Map、Stack、Vector、List、Array
- Python集合(set)类型的操作
- STL_Algorithm10: Set--集合操作
- Scala学习笔记--集合类型Queue,Set
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- Python集合(set)类型的基本操作
- Java集合工具类(二)-泛型集合工具类,用于便捷快速的定义、操作集合。包含Set的交集、并集...
- spring利用set操作对引用类型和集合类型进行注入值
- Python集合(set)类型的操作
- Python集合(set)类型的操作
- NoSQL之Redis对set(集合)数据类型的操作之二
- Python集合(set)类型的操作
- 行、重复-SAP HANA 集合操作 UNION/Union all/INTERSECT/EXCEPT (SAP HANA Set Operations)-by小雨
- scala对Set的操作
- Python集合(set)类型的操作
- Python 集合set添加删除、交集、并集、集合操作符号