I'm looking for a Java collection, possibly in standard library, which is able to collect the following structure:
class Item {
String key;
double score;
}
And with the following properties:
- Only one Item with the same key is allowed (like a set)
- insert, remove, check existance in max O(logn)
- traversal ordered by score, finding next in max O(logn)
As far as I understood standard OrderedSet must have comparable interface coherent with equals() interface, but that is not my case as two items with different key may have the same score.
In fact I've notice that TreeSet uses the comparator returning 0 to check if the item is already present.
Any suggestion?
Now that insert has been relaxed to O(log n), you can do this with a double-set, i.e. implement your own set that maintains 2 sets behind the scenes.
Best would be if you can modify class
Itemto implementequals()andhashCode()to only use thekeyfield. In that case your class would use aHashSetand aTreeSet. IfhashCode()covers more than just thekeyfield, then use twoTreeSetobjects.