I need a collection that does not allow for duplicates, is sorted, and supports the subList(startIndex, endIndex) method. Does one exist, and if it does, what collection is it?
TreeMap has no duplicates, has sorting, but doesn’t have subList(index, index). ArrayList has subList(index, index), but it can contain duplicated values. Are there any options?
I believe that you’re looking for something called an order-statistic tree, which is a balanced binary search tree augmented with extra information so that, in O(log n) time, you can query the element at any index. Because it’s a binary search tree, it doesn’t allow duplicates, and because you can query for the element at a given position, you can efficiently implement subranges; just hand back a new view of the tree where you keep track of the start and end index, so when iterating over the tree you can span that particular range.
The Java standard libraries do not provide an implementation of this data structure (I actually don’t know of any language that does provide it). I managed to find (on Google’s cache) this implementation of order statistic trees, which contains two files:
Hope this helps!