Documentation
¶
Index ¶
- type StringSet
- func (s StringSet) Add(elem string) bool
- func (s StringSet) AddList(elems ...string) int
- func (s StringSet) AddSet(set StringSet)
- func (s StringSet) Clear()
- func (s StringSet) Contains(elem string) bool
- func (s StringSet) Copy() StringSet
- func (s StringSet) Equals(set interface{}) bool
- func (s StringSet) Foreach(f func(v string) bool)
- func (s StringSet) IsEmpty() bool
- func (s StringSet) Remove(elem string) bool
- func (s StringSet) RemoveSet(set StringSet)
- func (s StringSet) RetainSet(set StringSet)
- func (s StringSet) Size() int
- func (s StringSet) String() string
- func (s StringSet) ToList() []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type StringSet ¶
type StringSet map[string]struct{}
StringSet 定义string集合
Example ¶
package main
import (
"fmt"
"github.com/recallsong/go-utils/container/set/stringset"
"github.com/recallsong/go-utils/container/slice/strings"
)
func main() {
set := stringset.StringSet{}
// 向集合添加多个元素
set.AddList("a", "b", "c", "d", "c", "c", "d")
// 向集合添加单个元素
set.Add("e")
// 排序并打印
fmt.Println(strings.Strings(set.ToList()).Sort())
// 集合运算
s1 := stringset.StringSet{}
s1.AddList("a", "b", "c")
s2 := stringset.StringSet{}
s2.AddList("c", "d", "e")
// 并集
s3 := s1.Copy()
s3.AddSet(s2)
fmt.Println(strings.Strings(s3.ToList()).Sort())
// 差集
s3 = s1.Copy()
s3.RemoveSet(s2)
fmt.Println(strings.Strings(s3.ToList()).Sort())
// 交集
s3 = s1.Copy()
s3.RetainSet(s2)
fmt.Println(strings.Strings(s3.ToList()).Sort())
}
Output: [a b c d e] [a b c d e] [a b] [c]
Click to show internal directories.
Click to hide internal directories.