2

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

2 Answers 2

8

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:

Map<MyObject, TreeSet<MyObject>> map = new HashMap<MyObject, TreeSet<MyObject>>();

void addToSet(MyObject obj, TreeSet<MyObject> set) {
    TreeSet<MyObject> otherSet = map.get(obj);
    if (otherSet != null) {
        otherSet.remove(obj);
    }
    set.add(obj);
    map.put(obj, set);
}

void removeFromSet(MyObject obj, TreeSet<MyObject> set) {
    set.remove(obj);
    map.remove(obj);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure I follow, do you have a little code snippet or algorithm for reference?
0

maybe something like:

import java.util.*;
class Sets {
    Sets(TreeSet<Integer> set1, TreeSet<Integer> set2, TreeSet<Integer> set3) {
        this.set1 = set1;
        this.set2 = set2;
        this.set3 = set3;
    }
    boolean add(TreeSet<Integer> set, Integer i) {
        if (set == set1) {
            if (set2.contains(i)) {
                set2.remove(i);
                return set.add(i);
            } else if (set3.contains(i)) {
                set3.remove(i);
                return set.add(i);
            } else return set.add(i);
        }
        else if (set == set2) {
            if (set1.contains(i)) {
                set1.remove(i);
                return set.add(i);
            } else if (set3.contains(i)) {
                set3.remove(i);
                return set.add(i);
            } else return set.add(i);
        }
        else if (set == set3) {
            if (set1.contains(i)) {
                set1.remove(i);
                return set.add(i);
            } else if (set2.contains(i)) {
                set2.remove(i);
                return set.add(i);
            } else return set.add(i);
        }
        return false;
    }
    final TreeSet<Integer> set1, set2, set3;
}
public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> set1 = new TreeSet<Integer>();
        set1.add(1);
        TreeSet<Integer> set2 = new TreeSet<Integer>();
        set2.add(2);
        TreeSet<Integer> set3 = new TreeSet<Integer>();
        Sets sets = new Sets(set1, set2, set3);
        System.out.println(set1);
        System.out.println(set2);
        System.out.println(set3);
        sets.add(set3,1);
        sets.add(set3,2);
        sets.add(set3,3);
        System.out.println(set1);
        System.out.println(set2);
        System.out.println(set3);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.