一、Set
### --- Set
~~~ Set(集合)是没有重复元素的对象集合,Set中的元素是唯一的;
~~~ Set分为可变的和不可变的集合;
~~~ 默认情况下,使用的是不可变集合(引用 scala.collection.immutable.Set);
~~~ 使用可变集合,需要引用 scala.collection.mutable.Set 包;
### --- 编程示例
object SetDemo {
def main(args: Array[String]): Unit = {
// 判断元素是否存在
val set = Set(1, 2, 3, 4, 5, 6, 7)
println(set.exists(_ % 2 == 0))
// 删除元素
set.drop(1)
// 引入可变的Set
import scala.collection.mutable.Set
val mutableSet = Set(4, 5, 6)
// 增加元素、删除元素;执行成功返回true,否则返回false
mutableSet.add(7)
println(mutableSet)
mutableSet.remove(7)
println(mutableSet)
// 使用 += / -= 增加、删除元素,表达更简洁
mutableSet += 5
mutableSet -= 2
// 集合典型操作交、并、差
//交集(&、intersect)
println(Set(1, 2, 3) & Set(2, 3, 4))
println(Set(1, 2, 3).intersect(Set(2, 3, 4)))
println(Set(1, 2, 3) intersect (Set(2, 3, 4)))
//并集(++、|、union)
println(Set(1, 2, 3) ++ Set(2, 3, 4))
println(Set(1, 2, 3) | Set(2, 3, 4))
println(Set(1, 2, 3).union(Set(2, 3, 4)))
//差集(--、&~、diff)
//返回:包含本集合中不包含在给定集合中的元素的集合
println(Set(1, 2, 3) -- Set(2, 3, 4))
println(Set(1, 2, 3) &~ Set(2, 3, 4))
println(Set(1, 2, 3).diff(Set(2, 3, 4)))
}
}
二、编程实现
### --- 编程代码
package yanqi.cn.part09
object SetDemo {
def main(args: Array[String]): Unit = {
//创建一个Set集合
val set = Set(1, 2, 3, 4, 5, 6)
set.drop(1)
println(set)
//创建一个可变的Set
import scala.collection.mutable.Set
val mutableSet = Set(3, 4, 5)
//对可变的Set进行增加元素、删除元素的操作
mutableSet.add(7)
println(mutableSet)
mutableSet.remove(7)
println(mutableSet)
//通过使用+= -=进行增加、删除元素的操作
mutableSet += 8
mutableSet -= 3
println(mutableSet)
//对Set集合进行交集的操作(& intersect)
println(Set(1, 2, 3) & Set(2, 3, 4))
println(Set(1, 2, 3).intersect(Set(2, 3, 4)))
//对Set集合进行并集的操作(++ | union)
println(Set(1,2,3) ++ Set(2,3,4))
println(Set(1,2,3) | Set(2,3,4))
println(Set(1,2,3).union(Set(2,3,4)))
//对Set集合进行差集的操作(-- &~ diff)
println(Set(1,2,3) -- Set(2,3,4))
println(Set(1,2,3) &~ Set(2,3,4))
println(Set(1,2,3).diff(Set(2,3,4)) )
}
}
### --- 编译打印
D:\JAVA\jdk1.8.0_231\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=60255:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\ScalaPro\out\production\ScalaPro;D:\JAVA\scala-2.12.2\lib\scala-library.jar;D:\JAVA\scala-2.12.2\lib\scala-reflect.jar yanqi.cn.part09.SetDemo
Set(5, 1, 6, 2, 3, 4)
Set(5, 3, 7, 4)
Set(5, 3, 4)
Set(5, 4, 8)
Set(2, 3)
Set(2, 3)
Set(1, 2, 3, 4)
Set(1, 2, 3, 4)
Set(1, 2, 3, 4)
Set(1)
Set(1)
Set(1)
Process finished with exit code 0