Skip to content
本页内容

# Set ​

Set 集合数据结构，类似列表。Set 中元素不重复。

## 源码 ​

- https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go

## 用法 ​

go

    import (
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    import (
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

## 目录 ​

- NewSet
- NewSetFromSlice
- Values
- Add
- AddIfNotExist
- AddIfNotExistBy
- Delete
- Contain
- ContainAll
- Clone
- Size
- Equal
- Iterate
- IsEmpty
- Union
- Intersection
- SymmetricDifference
- Minus

## 文档 ​

### NewSet ​

返回Set结构体对象

函数签名:

go

    type Set[T comparable] map[T]bool
    func NewSet[T comparable](items ...T) Set[T]

    type Set[T comparable] map[T]bool
    func NewSet[T comparable](items ...T) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

### NewSetFromSlice ​

基于切片创建集合

函数签名:

go

    func NewSetFromSlice[T comparable](items []T) Set[T]

    func NewSetFromSlice[T comparable](items []T) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSetFromSlice([]int{1, 2, 2, 3})
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSetFromSlice([]int{1, 2, 2, 3})
        fmt.Println(st.Values()) //1,2,3
    }

### Values ​

获取集合中所有元素的切片

函数签名:

go

    func (s Set[T]) Values() []T

    func (s Set[T]) Values() []T

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int](1,2,2,3)
        fmt.Println(st.Values()) //1,2,3
    }

### Add ​

向集合中添加元素

函数签名:

go

    func (s Set[T]) Add(items ...T)

    func (s Set[T]) Add(items ...T)

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Values()) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Values()) //1,2,3
    }

### AddIfNotExist ​

如果集合中不存在元素，则添加该元素返回true, 如果集合中存在元素, 不做任何操作，返回false

函数签名:

go

    func (s Set[T]) AddIfNotExist(item T) bool

    func (s Set[T]) AddIfNotExist(item T) bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        r1 := st.AddIfNotExist(1)
        r2 := st.AddIfNotExist(4)

        fmt.Println(r1) // false
        fmt.Println(r2) // true
        fmt.Println(st.Values()) // 1,2,3,4
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        r1 := st.AddIfNotExist(1)
        r2 := st.AddIfNotExist(4)

        fmt.Println(r1) // false
        fmt.Println(r2) // true
        fmt.Println(st.Values()) // 1,2,3,4
    }

### AddIfNotExistBy ​

根据checker函数判断元素是否在集合中，如果集合中不存在元素且checker返回true，则添加该元素返回true, 否则不做任何操作，返回false

函数签名:

go

    func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool

    func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2)

        ok := st.AddIfNotExistBy(3, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(ok) // true

        notOk := st.AddIfNotExistBy(4, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(notOk) // false

        fmt.Println(st.Values()) // 1, 2, 3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2)

        ok := st.AddIfNotExistBy(3, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(ok) // true

        notOk := st.AddIfNotExistBy(4, func(val int) bool {
            return val%2 != 0
        })
        fmt.Println(notOk) // false

        fmt.Println(st.Values()) // 1, 2, 3
    }

### Delete ​

删除集合中元素

函数签名:

go

    func (s Set[T]) Delete(items ...T)

    func (s Set[T]) Delete(items ...T)

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        set.Delete(3)
        fmt.Println(st.Values()) //1,2
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        set.Delete(3)
        fmt.Println(st.Values()) //1,2
    }

### Contain ​

判断集合是否包含某个值

函数签名:

go

    func (s Set[T]) Contain(item T) bool

    func (s Set[T]) Contain(item T) bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Contain(1)) //true
        fmt.Println(st.Contain(4)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        st := set.NewSet[int]()
        st.Add(1, 2, 3)

        fmt.Println(st.Contain(1)) //true
        fmt.Println(st.Contain(4)) //false
    }

### ContainAll ​

判断集合是否包含另一个集合

函数签名:

go

    func (s Set[T]) ContainAll(other Set[T]) bool

    func (s Set[T]) ContainAll(other Set[T]) bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.ContainAll(set2)) //true
        fmt.Println(set1.ContainAll(set3)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.ContainAll(set2)) //true
        fmt.Println(set1.ContainAll(set3)) //false
    }

### Size ​

获取集合中元素的个数

函数签名:

go

    func (s Set[T]) Size() int

    func (s Set[T]) Size() int

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)

        fmt.Println(set1.Size()) //3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)

        fmt.Println(set1.Size()) //3
    }

### Clone ​

克隆一个集合

函数签名:

go

    func (s Set[T]) Clone() Set[T]

    func (s Set[T]) Clone() Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set1.Clone()

        fmt.Println(set1.Size() == set2.Size()) //true
        fmt.Println(set1.ContainAll(set2)) //true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set1.Clone()

        fmt.Println(set1.Size() == set2.Size()) //true
        fmt.Println(set1.ContainAll(set2)) //true
    }

### Equal ​

比较两个集合是否相等，包含相同元素为相等

函数签名:

go

    func (s Set[T]) Equal(other Set[T]) bool

    func (s Set[T]) Equal(other Set[T]) bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2, 3)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.Equal(set2)) //true
        fmt.Println(set1.Equal(set3)) //false
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(1, 2, 3)
        set3 := set.NewSet(1, 2, 3, 4)

        fmt.Println(set1.Equal(set2)) //true
        fmt.Println(set1.Equal(set3)) //false
    }

### Iterate ​

迭代结合，在每个元素上调用函数

函数签名:

go

    func (s Set[T]) Iterate(fn func(item T))

    func (s Set[T]) Iterate(fn func(item T))

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        arr := []int{}
        set.Iterate(func(item int) {
            arr = append(arr, item)
        })

        fmt.Println(arr) //1,2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        arr := []int{}
        set.Iterate(func(item int) {
            arr = append(arr, item)
        })

        fmt.Println(arr) //1,2,3
    }

### EachWithBreak ​

遍历集合的元素并为每个元素调用iteratee函数，当iteratee函数返回false时，终止遍历。

函数签名:

go

    func (s Set[T]) EachWithBreak(iteratee func(item T) bool)

    func (s Set[T]) EachWithBreak(iteratee func(item T) bool)

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet(1, 2, 3, 4, 5)

        var sum int

        s.EachWithBreak(func(n int) bool {
            if n > 3 {
                return false
            }
            sum += n
            return true
        })

        fmt.Println(sum) //6
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet(1, 2, 3, 4, 5)

        var sum int

        s.EachWithBreak(func(n int) bool {
            if n > 3 {
                return false
            }
            sum += n
            return true
        })

        fmt.Println(sum) //6
    }

### IsEmpty ​

判断集合是否为空

函数签名:

go

    func (s Set[T]) IsEmpty() bool

    func (s Set[T]) IsEmpty() bool

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet()

        fmt.Println(set1.IsEmpty()) //false
        fmt.Println(set2.IsEmpty()) //true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet()

        fmt.Println(set1.IsEmpty()) //false
        fmt.Println(set2.IsEmpty()) //true
    }

### Union ​

求两个集合的并集

函数签名:

go

    func (s Set[T]) Union(other Set[T]) Set[T]

    func (s Set[T]) Union(other Set[T]) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Union(set2)

        fmt.Println(set3.Values()) //1,2,3,4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Union(set2)

        fmt.Println(set3.Values()) //1,2,3,4,5
    }

### Intersection ​

求两个集合的交集

函数签名:

go

    func (s Set[T]) Intersection(other Set[T]) Set[T]

    func (s Set[T]) Intersection(other Set[T]) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Intersection(set2)

        fmt.Println(set3.Values()) //2,3
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.Intersection(set2)

        fmt.Println(set3.Values()) //2,3
    }

### SymmetricDifference ​

返回一个集合，其中元素在第一个集合或第二个集合中，且不同时存在于两个集合中

函数签名:

go

    func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]

    func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.SymmetricDifference(set2)

        fmt.Println(set3.Values()) //1,4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set1.SymmetricDifference(set2)

        fmt.Println(set3.Values()) //1,4,5
    }

### Minus ​

创建一个集合，其元素在原始集中但不在比较集中

函数签名:

go

    func (s Set[T]) Minus(comparedSet Set[T]) Set[T]

    func (s Set[T]) Minus(comparedSet Set[T]) Set[T]

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set.NewSet(2, 3)

        res1 := set1.Minus(set2)
        fmt.Println(res1.Values()) //1

        res2 := set2.Minus(set3)
        fmt.Println(res2.Values()) //4,5
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        set1 := set.NewSet(1, 2, 3)
        set2 := set.NewSet(2, 3, 4, 5)
        set3 := set.NewSet(2, 3)

        res1 := set1.Minus(set2)
        fmt.Println(res1.Values()) //1

        res2 := set2.Minus(set3)
        fmt.Println(res2.Values()) //4,5
    }

### Pop ​

删除并返回集合中的顶部元素

函数签名:

go

    func (s Set[T]) Pop() (v T, ok bool)

    func (s Set[T]) Pop() (v T, ok bool)

示例:

go

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet[int]()
        s.Add(1)
        s.Add(2)
        s.Add(3)

        val, ok = s.Pop()

        fmt.Println(val) // 3
        fmt.Println(ok) // true
    }

    package main

    import (
        "fmt"
        set "github.com/duke-git/lancet/v2/datastructure/set"
    )

    func main() {
        s := set.NewSet[int]()
        s.Add(1)
        s.Add(2)
        s.Add(3)

        val, ok = s.Pop()

        fmt.Println(val) // 3
        fmt.Println(ok) // true
    }