Java番石榴Sets.difference行为:
Known = ["v1","v2"]; Incoming = ["v2","v3","v4"]
incoming = ["v2","v3","v4"]; knownUpdated = ["v2"]
Sets.difference(Known, Incoming) = v1 (To be removed)
Sets.difference(incoming, knownUpdated) = v3,v4 (To be added)
我在 Go 中尝试过的有以下区别:
Output := [v1 v3 v4] (known, Incoming)
func Difference(slice1 []string, slice2 []string) []string {
var diff []string
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
if !found {
diff = append(diff, s1)
}
}
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
它给出了对称的差异,但我需要 Guava sets.difference 的行为。我知道我的函数有问题。来自番石榴文档public static Sets.SetView difference(Set set1, Set set2):返回的集合包含由 set1 包含但由 set2 不包含的所有元素
天涯尽头无女友
相关分类