There are two ways to use a TreeSet
.
- Have it contain objects that implement
Comparable
- Have a custom
Comparator
object that compares the elements of your TreeSet
.
Since you want to have your TreeSet
contain BitSet
s, and BitSet
does not implement Comparable
, you need to give your TreeSet
a custom Comparator
. How you implement that Comparator
is up to you.
SortedSet<BitSet> s = new TreeSet<BitSet>(new CustomBitSetComparator());
s.add(bitSet1);
s.add(bitSet2);
//etc ...
The Comparator may look something like this
class CustomBitSetComparator implements Comparator<BitSet>{
int compare(BitSet a, BitSet b) {
if(a == b){
return 0;
} else if(a == null) {
return -1;
} else if(b == null) {
return 1;
} else if(a.equals(b)) {
return 0;
} else if(a.length() > b.length()) {
return 1;
} else if(b.lenght() > a.length()) {
return -1;
} else {
for(int i = 0; i < a.length(); i++) {
if(a.get(i) != b.get(i)) {
if(a.get(i)) {
return 1;
} else {
return -1;
}
}
}
return 0;
}
}
}
compareTo()
? Cardinality? – BertiBitSet
does not implementcompareTo
, so you have to define your own ordering ofBitSet
. What is your desired ordering? – Motionless