执行Set计算和判断

2021-08-15  本文已影响0人  一个栗

基本Set操作

基本Set操作

基本 Set 操作

let set:Set<Character> = ["A", "B", "C"]
let set2:Set<Character> = ["B", "E", "F", "G"]
print(set.intersection(set2))
print(set.union(set2))
print(set.symmetricDifference(set2))
print(set.subtracting(set2))

执行结果如下:
["B"]
["E", "B", "G", "A", "F", "C"]
["E", "G", "A", "F", "C"]
["A", "C"]

Set 判断方法

let smallSet:Set = [1, 2, 3]
let bigSet:Set = [1, 2, 3, 4]
print(smallSet.isSubset(of: bigSet))
print(bigSet.isSuperset(of: smallSet))
print(smallSet.isStrictSubset(of: bigSet))
print(bigSet.isStrictSuperset(of: smallSet))
print(smallSet.isDisjoint(with: bigSet))

执行结果如下:
true
true
true
true
false
上一篇 下一篇

猜你喜欢

热点阅读