stringset

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

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]

func (StringSet) Add

func (s StringSet) Add(elem string) bool

Add 向集合添加元素,如果已存在,则返回false, 否则返回true

func (StringSet) AddList

func (s StringSet) AddList(elems ...string) int

AddList 向集合中添加一组元素,自动过滤重复元素,返回实际添加的元素数量

func (StringSet) AddSet

func (s StringSet) AddSet(set StringSet)

AddSet 集合并集

func (StringSet) Clear

func (s StringSet) Clear()

Clear 清空集合元素

func (StringSet) Contains

func (s StringSet) Contains(elem string) bool

Contains 判断集合是否包含该元素

func (StringSet) Copy

func (s StringSet) Copy() StringSet

Copy 对集合进行浅拷贝

func (StringSet) Equals

func (s StringSet) Equals(set interface{}) bool

Equals 对两个集合进行深度比较,返回是否相等

func (StringSet) Foreach

func (s StringSet) Foreach(f func(v string) bool)

Foreach 遍历集合,如果f返回 false ,则终止遍历

func (StringSet) IsEmpty

func (s StringSet) IsEmpty() bool

IsEmpty 判断集合是否为空

func (StringSet) Remove

func (s StringSet) Remove(elem string) bool

Remove 从集合中删除元素,如果不存在,则返回false, 否则返回true

func (StringSet) RemoveSet

func (s StringSet) RemoveSet(set StringSet)

removeSet 集合差集

func (StringSet) RetainSet

func (s StringSet) RetainSet(set StringSet)

retainAll 集合交集

func (StringSet) Size

func (s StringSet) Size() int

Size 返回集合的大小

func (StringSet) String

func (s StringSet) String() string

String 将集合输出为字符串

func (StringSet) ToList

func (s StringSet) ToList() []string

ToList 将集合转换为[]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL