Java Guava Sets.difference
behavior:
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)
What I have tried in Go is giving the below difference:
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
}
Its giving symmetric difference but I need the behavior of Guava sets.difference. I know something is wrong with my func. From the guava documentation of public static Sets.SetView difference(Set set1, Set set2)
:The returned set contains all elements that are contained by set1 and not contained by set2