Swift 5.x Set 访问和修改

2020-06-25  本文已影响0人  ShenYj

1.遍历 Set

let courses: Set = ["Math", "English", "History"]
for course in courses {
    print(course)
}
print("--- 分割线 ---")
for course in courses.sorted() {
    print(course)
}

输出结果:

English   
History
Math
--- 分割线 ---
English
History
Math

2. 访问Set

let set: Set<Character> = ["A", "B", "C"]
print(set.count)
print(set.isEmpty)

输出结果:

3
false

3. 添加元素

struct Person {
    var name: String = ""
    var age: Int = 0
}
extension Person: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
    }
}
extension Person: Equatable {
    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

var personSet: Set<Person> = [
    Person(name: "zhangsan", age: 23),
    Person(name: "lisi", age: 20)
]
personSet.update(with: Person(name: "zhangsan", age: 24))
print(personSet)

这里的Person 只计算name的哈希值, 并且只根据name判断两个Person对象是否相等

输出结果:

[
__lldb_expr_148.Person(name: "zhangsan", age: 24), 
__lldb_expr_148.Person(name: "lisi", age: 20)
]

由于这里只计算name的哈希值, 所以执行udpate时, 将原有的数据进行了更新, 而没有新增

如果改成

hasher.combine(name)
hasher.combine(age)

则会新增一条记录

[
__lldb_expr_150.Person(name: "zhangsan", age: 23), 
__lldb_expr_150.Person(name: "zhangsan", age: 24), 
__lldb_expr_150.Person(name: "lisi", age: 20)
]

4. 移除元素

struct Person {
    var name: String = ""
    var age: Int = 0
}
extension Person: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(age)
    }
}
extension Person: Equatable {
    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

var personSet: Set<Person> = [
    Person(name: "zhangsan", age: 23),
    Person(name: "lisi", age: 20),
    Person(name: "wangwu", age: 27)
]
let filterdSet = personSet.filter({$0.age < 25})
print(filterdSet)

输出结果:

[
__lldb_expr_152.Person(name: "zhangsan", age: 23), 
__lldb_expr_152.Person(name: "lisi", age: 20)
]
struct Person {
    var name: String = ""
    var age: Int = 0
}
extension Person: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(age)
    }
}
extension Person: Equatable {
    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

var personSet: Set<Person> = [
    Person(name: "zhangsan", age: 23),
    Person(name: "lisi", age: 20),
    Person(name: "wangwu", age: 27)
]
let removedElement: Person? = personSet.remove(Person(name: "zhangsan", age: 22))
print(removedElement ?? "集合不存在该元素")

由于age在哈希值计算范围内, 所以打印结果为

集合不存在该元素

如果去掉hasher.combine(age)这一行代码, 打印结果:

Person(name: "zhangsan", age: 23)
personSet.removeAll()
print(personSet)

输出结果:

[]
struct Person {
    var name: String = ""
    var age: Int = 0
}
extension Person: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
    }
}
extension Person: Equatable {
    static func == (lhs: Person, rhs: Person) -> Bool {
        return lhs.name == rhs.name
    }
}

var personSet: Set<Person> = [
    Person(name: "zhangsan", age: 23),
    Person(name: "lisi", age: 20),
    Person(name: "wangwu", age: 27)
]
personSet.removeFirst()
print(personSet)

输出结果:

[
__lldb_expr_175.Person(name: "lisi", age: 20), 
__lldb_expr_175.Person(name: "wangwu", age: 27)
]

5. 基于Set操作的

e.g.

let set1: Set<Character> = ["A", "B", "C"]
let set2: Set<Character> = ["B", "E", "F", "G"]
print("交集 AnB: " + set1.intersection(set2))
print("并集 AuB: " + set1.union(set2))
print("对称差集: " + set1.symmetricDifference(set2))
print("相对补集 A-B: " + set1.subtracting(set2))```

输出结果:

交集 AnB: B
并集 AuB: FGBCAE
对称差集: GCEAF
相对补集 A-B: AC

5. 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
上一篇下一篇

猜你喜欢

热点阅读