I'm trying to find a good approach to solve quite simple problem. For example, let us say that there are three sets (there might be more):
Set<Integer> A = [1,2,3,4,5,6]
Set<Integer> B = [2,4,6,8,10]
Set<Integer> C = [1,3,6,9]
Now, how to find answer to this equation:
(A or B) and (B or C)
.
Basically and and or are intersection and union.
So the answer to this would be just a single set.
The answer to above operation would be:
[1,2,3,4,6,8,10]
I have done methods that find union and intersection with two sets, but I'm stuck in the point where there might be more than one operation in the equation. And the operation length depends on the user input. It might be different each time
// intersection
private Set<Integer> intersection(Set setA, Set setB){
return (Set<Integer>) setA.stream()
.filter(setB::contains)
.collect(Collectors.toSet());
}
// union
private Set<Integer> union(Set setA, Set setB){
Set<Integer> result = new HashSet<Integer>(setA);
result.addAll(setB);
return result;
}
All approaches in Stackoverflow are based on two sets...