I’d like to take the intersection of a set and a range, so that I get a set containing every element that is not in the range. For example, I’d like a way to take set and range from the following code snippet:
import com.google.common.collect.*;
TreeSet<Integer> set = Sets.newTreeSet();
Collections.addAll(set, 1,2,3,5,11);
Range<Integer> range = Range.closed(4,10);
and return a new TreeSet containing just 5
In this particular example, you’re better off not using
Rangeat all, but usingset.subSet(4, true, 10, true)directly, but presumably you have a more complicated use case, and your code is a simplified example.There’s really not much alternative but to deal with all the cases yourself. Part of the problem is that a
NavigableSetcan use an arbitraryComparator, butRange(deliberately) works only with the natural ordering of the value type, so it’d be somewhat awkward to provide a method in Guava that takes an arbitraryRangeand aNavigableSetand intersects them.The most general solution would look something like…
That said, it’s worth mentioning that if you’re not concerned about efficiency, you can just do
Iterables.removeIf(set, Predicates.not(range))orSets.filter(set, range).