In order so reduce memory consumption, I'm rewriting a class that has a SortedSet<Integer>. In 80% of cases, this collection contains only a single element . So I thought I could use a SingeltonSet in these cases and a normal TreeSet in other cases. Now I noticed that SingletonSet, as returned by Collections.singleton(), does not implement SortedSet. Is there any reason for this shortcoming? A single element can always be considered sorted I'd say. Do I have to write my own SingletonSet implementation?
The SortedSet interface defines methods that require a Comparator for the set elements. Thus elements maintained by a SortedSet have to be comparable. If the singleton returned by Collections.singleton() would implement SortedSet, then Collections.singleton() could accept Comparables only (which is not what we want).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With