I have 3 different TreeSet objects in my application, all of which store the same class of object.
However, at any one point of time, a single object may exist in only one of the TreeSets.
So, if I queried each TreeSet using contains() for a particular object it should exist in only one of them.
Is there an easy way to add this kind of logic to my TreeSet easily? Obviously I can perform the above contains() check myself after each operation but just wondering if there are better ways?
Thanks
You can keep a map that maps objects to the TreeSet to which they currently belong. If an object has no entry, it can be added to a TreeSet; otherwise it must be removed from the mapped TreeSet before it assigned to a new one.
EDIT:
Per your request, here’s a code snippet for how this might be done: