Is there an existing List implementation in Java that maintains order based on provided Comparator?
Something that can be used in the following way:
Comparator<T> cmp = new MyComparator<T>();
List<T> l = new OrderedList<T>(cmp);
l.add(someT);
so that someT gets inserted such that the order in the list is maintained according to cmp
(On @andersoj suggestion I am completing my question with one more request)
Also I want to be able to traverse the list in sorted order without removing the elements, i.e:
T min = Const.SMALLEST_T;
for (T e: l) {
assertTrue(cmp.compare(min, e) >= 0);
min = e;
}
should pass.
All suggestions are welcome (except telling me to use Collections.sort on the unordered full list), though, I would prefer something in java.* or eventually org.apache.* since it would be hard to introduce new libraries at this moment.
Note: (UPDATE4) I realized that implementations of this kind of list would have inadequate performance. There two general approaches:
- Use Linked structure (sort of) B-tree or similar
- Use array and insertion (with binary search)
No 1. has problem with CPU cache misses No 2. has problem with shifting elements in array.
UPDATE2:
TreeSet does not work because it uses the provided comparator (MyComparator) to check for equality and based on it assumes that the elements are equal and exclude them. I need that comparator only for ordering, not "uniqueness" filtering (since the elements by their natural ordering are not equal)
UPDATE3:
PriorityQueue does not work as List (as I need) because there is no way to traverse it in the order it is "sorted", to get the elements in the sorted order you have to remove them from the collection.
UPDATE:
Similar question:
A good Sorted List for Java
Sorted array list in Java
Response to new requirement. I see two potentials:
Do what the JavaDoc for
PriorityQueuesays:I suspect this will yield the best performance given your requirements. If this is not acceptable, you'll need to better explain what you're trying to accomplish.
Build a List that simply sorts itself upon addition of new elements. This is a real pain... if you used a linked structure, you can do an efficient insertion sort, but locality is bad. If you used an array-backed structure, insertion sort is a pain but traversal is better. If iteration/traversal is infrequent, you could hold the list contents unsorted and sort only on demand.
Consider using a PriorityQueue as I suggested, and in the event you need to iterate in order, write a wrapper iterator:
Use Guava's
TreeMultiSet. I tested the following code withIntegerand it seems to do the right thing.The below addresses the uniqueness/filtering problem you were having when using a
SortedSet. I see that you also want an iterator, so this won't work.If what you really want is an ordered list-like thing, you can make use of a
PriorityQueue.Take note of what the API documentation says about the time properties of various operations:
You should also be aware that the iterators produced by
PriorityQueuedo not behave as one might expect:I just noticed that Guava provides a
MinMaxPriorityQueue. This implementation is array-backed, rather than the linked form provided in the JDK'sPriorityQueue, and thus likely has different timing behavior. If you're doing something performance sensitive, you may wish to take a look. While the notes give slightly different (linear and logarithmic) big-O times, all those times should also be bounded, which may be useful.There is not a
Listimplementation per se that maintains ordering, but what you are likely looking for are implementations ofSortedSet. ATreeSetis the most common. The other implementation, aConcurrentSkipListSetis for more specific uses. Note that aSortedSetprovides ordering, but does not allow duplicate entries, as does aList.Refs:
PriorityQueueiterator is not ordered