Set

2023/1/29

# 基本使用

  • Swift 的所有基本类型(比如 String、Int、Double 和 Bool)默认都是可哈希化的,一个set只接受一种类型
  • 集合没有等价的简化形式。let set: Set<String>
    let s = Set([1, 1, 2, 3, 2, 3, 2])
    print(s); // [2, 3, 1]
    
    1
    2
  • 多种定义方式
    var s3: Set = ["h", "a", "b", "c"];
    var s2 = Set(["h", "a", "b", "c"]);
    var s1: Set<String> = ["h", "a", "b", "c"];
    
    1
    2
    3

# count

var s = Set([1, 3])

print(s.count); // 2
1
2
3

# isEmpty

var s = Set([1, 2, 3])

print(s.isEmpty); // false
1
2
3

# 遍历

  • Set遍历是无序的
var s = Set([1, 2, 3])

for i in s {
    print(i); // 2 1 3
}
1
2
3
4
5
  • 排序遍历
var s = Set([1, 2, 3])

for i in s.sorted() {
    print(i); // 1 2 3
}
1
2
3
4
5

# 增删改查

# insert

var s = Set([1, 3])

s.insert(2)
print(s); // [3, 2, 1]
1
2
3
4

# remove

var s = Set([1, 2, 3])

print(s.remove(3)); // Optional(3)
print(s);           // [2, 1]
1
2
3
4

# removeAll

var s = Set([1, 2, 3])

s.removeAll()
print(s);            // []
1
2
3
4

# contains

  • 简单包含
    let s = Set([1, 2, 3])
    
    let ans = s.contains(2)
    print(ans); // true
    
    1
    2
    3
    4
  • 复杂条件
    let s = Set([1, 3])
    
    let ans = s.contains(where: {$0 % 2 == 0})
    print(ans); // false
    
    1
    2
    3
    4

# 交并差补(集)

# intersection

  • 交集
var s1 = Set([1, 2, 3])
var s2 = Set([3, 4, 5])

print(s1.intersection(s2)); // [3]
1
2
3
4

# union

  • 并集
var s1 = Set([1, 2, 3])
var s2 = Set([3, 4, 5])

print(s1.union(s2)); // [4, 1, 5, 2, 3]
1
2
3
4

# symmetricDifference

  • 差集
var s1 = Set([1, 2, 3])
var s2 = Set([3, 4, 5])

print(s1.symmetricDifference(s2)); // [4, 5, 1, 2]
1
2
3
4

# subtracting

  • 补集
var s1 = Set([1, 2, 3])
var s2 = Set([3, 4, 5])

print(s1.subtracting(s2)); // [1, 2]
// s1 的 1,2 是 s2的补集
1
2
3
4
5

# 其他判断

# 相等判断

var s1 = Set([1, 2, 3])
var s2 = Set([1, 2, 3])

print(s1 == s2); // true
1
2
3
4

# 子集

var s1 = Set([1, 2, 3])
var s2 = Set([1, 23])

// 子集判断
print(s2.isSubset(of: s1)); // true
print(s1.isSuperset(of: s2)); // true

// 子集且不相等判断
print(s2.isStrictSubset(of: s1)); // false
print(s1.isStrictSuperset(of: s2)); // false
1
2
3
4
5
6
7
8
9
10

# 不相交判断

var s1 = Set([1, 2, 3])
var s2 = Set([4, 5])

print(s1.isDisjoint(with: s2)); // true
1
2
3
4

# 其他操作

# randomElement

var s1 = Set([1, 2, 3])

print(s1.randomElement()); // 2
1
2
3

# sorted

  • 不改变原 Set
var s1 = Set([1, 2, 3])

print(s1.sorted(by: {$0 < $1})); // [1, 2, 3]
1
2
3
上次更新: 11/1/2024