You can use retainAll method:
columnsOld.retainAll (columnsNew);
Answer from Roman on Stack OverflowYou can use retainAll method:
columnsOld.retainAll (columnsNew);
Using Google's Guava library:
Sets.intersection(Sets.newHashSet(setA), Sets.newHashSet(setB))
Note: This is much more efficient than naively doing the intersection with two lists: it's O(n+m), versus O(n×m) for the list version. With two million-item lists it's the difference between millions of operations and trillions of operations.
intersection for multiple lists - Java Programming
Finding the XOR between multiple lists of no overlapping time intervals (Java)
sets - Is there a simple way to perform the opposite of intersection on two lists? - Mathematica Stack Exchange
Intersection of more than two sets
One (slightly annoying) approach is to do the same thing as implementations of those functions, which for HashSet can be done like
set1.iter().filter(|k| set2.contains(k)).filter(|k| set3.contains(3))...
Or, like,
let sets = [&set2, &set3, ...]; set1.iter().filter(|k| sets.all(|s| s.contains(k)))
(This could probably be optimised by choosing set1 to be the one with the fewest elements.)
The BTreeMap version is a little more complicated: uses the normal iters and walks over them at the same time, using the fact that they're in sorted order to be more efficient than doing a log n contains check for each element (similar to the merge step of a merge sort). I'm not sure of a pithy way like the filter above to implement this.
Does any one have any elegant solution for finding the XOR between two lists of time intervals, preferably in Java? You can assume that, within each list, the time intervals are non-overlapping, but it is not safe to assume that between the two lists they are non-overlapping. I’ve been struggling with it the past couple of days.
There is probably a build-in command for this, but with over 6,000 commands, hard to search now. But you could try
inter=Intersection[list1,list2];
Complement[Union[list1,list2],inter]

a = {1, 2, 4, 7, 9};
b = {1, 4, 9, 10, 11};
Since V 13.1 we have SymmetricDifference, UniqueElements and
DeleteElements
SymmetricDifference[a, b]
{2, 7, 10, 11}
UniqueElements[{a, b}]
{{2, 7}, {10, 11}}
{DeleteElements[a, b], DeleteElements[b, a]}
{{2, 7}, {10, 11}}