我这边有个场景是针对数据去重,但又不是简单的去重,是有时间区间范围内的交集、并集计算。
废话不多说,我估计有不少人记不清集合的并集、差集、交集的概念了。
集合的分类:
并集:以属于A或属于B的元素为元素的集合成为A与B的并(集)
交集: 以属于A且属于B的元素为元素的集合成为A与B的交(集)
差集:以属于A而不属于B的元素为元素的集合成为A与B的差 (集)
如果只是去重的化,完全可以使用golang map实现。比如下面的例子就可以实现去重。
1
2
3
4
5
6
7
|
m
:
=
map
[
string
]
int
{
"baidu.com"
:
0
,
"apple.com"
:
0
,
"google.com"
:
0
,
"google.com"
:
0
,
}
|
和我们预料的一样,必须出错 !
1
2
3
4
5
|
# command-line-arguments
.
/
s
.
go
:
13
:
duplicate
key
"google.com"
in
map
literal
exit
status
2
|
golang-set是我在github找到的一个库,其实也没得选…. go get 安装go库包时候因为参数不对,总是失败….
1
2
3
4
5
6
7
8
9
10
|
#http://xiaorui.cc
$
go
get
https
:
/
/
github
.
com
/
deckarep
/
golang
-
set
.
git
package
https
:
/
github
.
com
/
deckarep
/
golang
-
set
.
git
:
"https://"
not
allowed
in
import
path
$
go
get
github
.
com
/
deckarep
/
golang
-
set
.
git
package
github
.
com
/
deckarep
/
golang
-
set
.
git
:
invalid
version
control
suffix
in
github
.
com
/
path
# ruifengyun at xiaorui in ~ [22:12:56]
$
go
get
github
.
com
/
deckarep
/
golang
-
set
|
下面是go set的使用实例.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package
main
import
(
"fmt"
"github.com/deckarep/golang-set"
)
func
main
(
)
{
kide
:
=
mapset
.
NewSet
(
)
kide
.
Add
(
"xiaorui.cc"
)
kide
.
Add
(
"blog.xiaorui.cc"
)
kide
.
Add
(
"vps.xiaorui.cc"
)
kide
.
Add
(
"linode.xiaorui.cc"
)
special
:
=
[
]
interface
{
}
{
"Biology"
,
"Chemistry"
}
scienceClasses
:
=
mapset
.
NewSetFromSlice
(
special
)
address
:
=
mapset
.
NewSet
(
)
address
.
Add
(
"beijing"
)
address
.
Add
(
"nanjing"
)
address
.
Add
(
"shanghai"
)
bonusClasses
:
=
mapset
.
NewSet
(
)
bonusClasses
.
Add
(
"Go Programming"
)
bonusClasses
.
Add
(
"Python Programming"
)
/
/一个并集的运算
allClasses
:
=
kide
.
Union
(
scienceClasses
)
.
Union
(
address
)
.
Union
(
bonusClasses
)
fmt
.
Println
(
allClasses
)
/
/是否包含
"Cookiing"
fmt
.
Println
(
scienceClasses
.
Contains
(
"Cooking"
)
)
/
/
false
/
/两个集合的差集
fmt
.
Println
(
allClasses
.
Difference
(
scienceClasses
)
)
/
/
Set
{
Music
,
Automotive
,
Go
Programming
,
Python
Programming
,
Cooking
,
English
,
Math
,
Welding
}
/
/两个集合的交集
fmt
.
Println
(
scienceClasses
.
Intersect
(
kide
)
)
/
/
Set
{
Biology
}
/
/有多少基数
fmt
.
Println
(
bonusClasses
.
Cardinality
(
)
)
/
/
2
}
|
上面那段go代码运行的结果:
1
2
3
4
5
6
7
|
[
`
go
run
s
.
go
`
|
done
:
432.97906ms
]
Set
{
vps
.
xiaorui
.
cc
,
Biology
,
blog
.
xiaorui
.
cc
,
Go
Programming
,
Python
Programming
,
linode
.
xiaorui
.
cc
,
Chemistry
,
xiaorui
.
cc
,
beijing
,
nanjing
,
shanghai
}
false
Set
{
linode
.
xiaorui
.
cc
,
xiaorui
.
cc
,
nanjing
,
vps
.
xiaorui
.
cc
,
blog
.
xiaorui
.
cc
,
Go
Programming
,
shanghai
,
Python
Programming
,
beijing
}
Set
{
}
2
|
set集合本来就没啥说的….