Re: Alternative to TreeSet?
On 06.05.2013 17:25, Daniel Pitts wrote:
On 5/6/13 8:10 AM, laredotornado@zipmail.com wrote:
We're using Java 6. Is there a java.util.Set data structure that can
return a sorted list of elements and can contain two elements even if
compareTo returns 0 against those two elements but calling equals
against the two elements returns false? TreeSet doesn't do the job.
In our example, we have products with an order ID column, so two
products could have the same order ID but may not be equal. We would
like to sort the products based on this order ID, however.
It sounds like you don't want a set, but a sorted bag. There may be
something in Apache Commons to solve this problem for you. Depending on
the size of the data set, I'd probably just use an ArrayList and call
Collections.sort().
And there are methods for binary search which will help with finding
element positions efficiently, e.g.
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#binarySearch(java.lang.Object[],
int, int, java.lang.Object)
I am not entirely sure though whether OP really wants a multiset (bag)
or just a custom ordering which takes more fields into account. It
depends on the operations executed on the collection.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/