码迷,mamicode.com
首页 > 其他好文 > 详细

Golang中使用set

时间:2020-06-28 15:35:32      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:union   https   ram   doc   参数   min   github   docke   print   

原文链接:https://studygolang.com/articles/27476?fr=sidebar

golang-set-A simple set type for the Go language. Also used by Docker, 1Password, Ethereum.
在github上已经有了一个成熟的包,名为golang-set,包中提供了线程安全和非线程安全的set。提供了五个set函数:

// NewSet创建并返回空集的引用,结果集上的操作是线程安全的
func NewSet(s ...interface{}) Set {}
// NewSetFromSlice从现有切片创建并返回集合的引用,结果集上的操作是线程安全的
func NewSetFromSlice(s []interface{}) Set {}
// NewSetWith创建并返回具有给定元素的新集合,结果集上的操作是线程安全的
func NewSetWith(elts ...interface{}) Set {}
// NewThreadUnsafeSet创建并返回对空集的引用,结果集上的操作是非线程安全的
func NewThreadUnsafeSet() Set {}
// NewThreadUnsafeSetFromSlice创建并返回对现有切片中集合的引用,结果集上的操作是非线程安全的。
func NewThreadUnsafeSetFromSlice(s []interface{}) Set {}

 

demo

package main

import (
    "fmt"
    "github.com/deckarep/golang-set"
)

func main() {
    // 默认创建的线程安全的,如果无需线程安全
    // 可以使用 NewThreadUnsafeSet 创建,使用方法都是一样的。
    s1 := mapset.NewSet(1, 2, 3, 4)
    fmt.Println("s1 contains 3: ", s1.Contains(3))
    fmt.Println("s1 contains 5: ", s1.Contains(5))

    // interface 参数,可以传递任意类型
    s1.Add("poloxue")
    fmt.Println("s1 contains poloxue: ", s1.Contains("poloxue"))
    s1.Remove(3)
    fmt.Println("s1 contains 3: ", s1.Contains(3))

    s2 := mapset.NewSet(1, 3, 4, 5)

    // 并集
    fmt.Println(s1.Union(s2))
}

运行结果:

s1 contains 3:  true
s1 contains 5:  false
s1 contains poloxue:  true
s1 contains 3:  false
Set{1, 2, 4, poloxue, 3, 5}

Examples but not exhaustive:

requiredClasses := mapset.NewSet()
requiredClasses.Add("Cooking")
requiredClasses.Add("English")
requiredClasses.Add("Math")
requiredClasses.Add("Biology")

scienceSlice := []interface{}{"Biology", "Chemistry"}
scienceClasses := mapset.NewSetFromSlice(scienceSlice)

electiveClasses := mapset.NewSet()
electiveClasses.Add("Welding")
electiveClasses.Add("Music")
electiveClasses.Add("Automotive")

bonusClasses := mapset.NewSet()
bonusClasses.Add("Go Programming")
bonusClasses.Add("Python Programming")

//Show me all the available classes I can take
allClasses := requiredClasses.Union(scienceClasses).Union(electiveClasses).Union(bonusClasses)
fmt.Println(allClasses) //Set{Cooking, English, Math, Chemistry, Welding, Biology, Music, Automotive, Go Programming, Python Programming}


//Is cooking considered a science class?
fmt.Println(scienceClasses.Contains("Cooking")) //false

//Show me all classes that are not science classes, since I hate science.
fmt.Println(allClasses.Difference(scienceClasses)) //Set{Music, Automotive, Go Programming, Python Programming, Cooking, English, Math, Welding}

//Which science classes are also required classes?
fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology}

//How many bonus classes do you offer?
fmt.Println(bonusClasses.Cardinality()) //2

//Do you have the following classes? Welding, Automotive and English?
fmt.Println(allClasses.IsSuperset(mapset.NewSetFromSlice([]interface{}{"Welding", "Automotive", "English"}))) //true

 

Golang中使用set

标签:union   https   ram   doc   参数   min   github   docke   print   

原文地址:https://www.cnblogs.com/wangjq19920210/p/13202586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!