3 scala集合-Set
与 Java 的 Set 一样,scala 的 set 中,元素都是唯一的,而且遍历 set 中集合的顺序,跟元素插入的顺序是不一样的。
同样,Set 也包含可变和不可变两种。要实现可变 Set 集合,需要使用类 scala.collection.mutable.Set
。
1 不可变 Set
创建不可变 Set 的语法如下:
// 创建一个不可变 Set
val/var variable_name = Set[类型]()
// 创建一个带初始化元素的不可变 Set
val/var variable_name = Set[类型](ele1, ele2, ele3, ...)
创建一个不可变 Set 集合
scala> val set = Set("Hello", "Scala", "Java", "JVM", "USA", "China")
val set: Set[String] = HashSet(JVM, USA, Scala, China, Hello, Java)
2 可变 Set
2.1 添加新元素到 Set
下面例子,定义了一个可变 Set,并添加新的元素到集合中:
scala> import scala.collection.mutable.Set
scala> val set = Set[String]("Hello", "Scala", "And", "Java", "!!!")
val set: scala.collection.mutable.Set[String] = HashSet(!!!, Java, Hello, And, Scala)
scala> set += "Spark"
val res1: scala.collection.mutable.Set[String] = HashSet(!!!, Java, Hello, And, Scala, Spark)
2.2 移除其中一个元素
下面例子,定义了一个可变 Set 集合,并移除其中的一个元素:
scala> val set = Set("Hadoop", "Hive", "Spark", "Flink")
val set: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink, Hadoop)
scala> set -= "Hadoop"
val res2: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink)
3 Set 常用操作
3.1 获取 Set 大小
通过 size 属性,可以获取 Set 集合包含的元素个数:
scala> import scala.collection.mutable.Set
scala> val set = Set("Hadoop", "Hive", "Spark", "Flink")
val set: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink, Hadoop)
scala> set.size
val res3: Int = 4
3.2 遍历 Set
与数组一样,使用 for 循环就可以遍历 Set:
scala> import scala.collection.mutable.Set
scala> val set = Set("Hadoop", "Hive", "Spark", "Flink")
val set: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink, Hadoop)
scala> for(i <- set)
| println(i)
|
Hive
Spark
Flink
Hadoop
3.3 添加一个元素,生成一个新 Set
使用运算符 +
,可以添加一个元素,并把结果生成一个新的 Set 集合对象:
scala> import scala.collection.mutable.Set
scala> val set = Set("Hadoop", "Hive", "Spark", "Flink")
val set: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink, Hadoop)
scala> val set1 = set + "Yarn"
val set1: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Yarn, Flink, Hadoop)
scala> set1
val res4: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Yarn, Flink, Hadoop)
scala> set
val res5: scala.collection.mutable.Set[String] = HashSet(Hive, Spark, Flink, Hadoop)
3.4 拼接集合
使用运算符 ++
可以拼接两个 Set 集合,并生成新的 Set 集合:
scala> import scala.collection.mutable.Set
scala> val set = Set("Hdfs", "Hadoop")
val set: scala.collection.mutable.Set[String] = HashSet(Hdfs, Hadoop)
scala> val set1 = Set("Hadoop", "Yarn")
val set1: scala.collection.mutable.Set[String] = HashSet(Yarn, Hadoop)
scala> val setCombined = set ++ set1
val setCombined: scala.collection.mutable.Set[String] = HashSet(Hdfs, Yarn, Hadoop)
除了拼接 Set 集合外,还可以拼接一个 Set 集合和一个 List 集合:
scala> import scala.collection.mutable.Set
scala> val set = Set("Hdfs", "Hadoop")
val set: scala.collection.mutable.Set[String] = HashSet(Hdfs, Hadoop)
scala> val list = List("Hadoop", "Yarn")
val list: List[String] = List(Hadoop, Yarn)
scala> val setCombined = set ++ list
val setCombined: scala.collection.mutable.Set[String] = HashSet(Hadoop, Hdfs, Yarn)