Scala 不可变集合Set

 1 package chapter07
 2 
 3 object Test06_ImmutableSet {
 4   def main(args: Array[String]): Unit = {
 5     // 1. 创建set 直接使用括号() apply的作用
 6     val set1 = Set(13, 23, 53, 12, 13, 23, 78)
 7     println(set1)
 8 
 9     println("==================")
10 
11     // 2. 添加元素 因为Set集合是无序的 所以直接使用+就可以,但是注意是集合+XXX 而不能反过来 因为Set的伴生对象实现了.+
12     val set2 = set1 + 129
13     println(set1)
14     println(set2)
15     println("==================")
16 
17     // 3. 合并set ++
18     val set3 = Set(19, 13, 23, 53, 67, 99)
19     val set4 = set2 ++ set3
20     println(set2)
21     println(set3)
22     println(set4)
23 
24     // 4. 删除元素 -
25     val set5 = set3 - 13
26     println(set3)
27     println(set5)
28   }
29 }

 

posted @ 2022-01-19 12:01  靠谱杨  阅读(75)  评论(0)    收藏  举报