问题
I am looking for a data structure that orders objects at insertion efficiently. I would like to order these objects (in this case individuals) based on the value of a particular variable (in this case the fitness).
The data structure should allow duplicate keys since a particular fitness value can occur in different individuals. This is a problem because for example the TreeMap data structure does not allow duplicate keys. I would prefer to use this type of tree-like structure because of it's efficiency O(log N).
If I inserted the individuals in an ordered list, the efficiency would drop to O(n), and sorting the individuals after they have been inserted wouldn't be very efficient either.
Is there a data structure that is efficient, keeps the individuals ordered and supports duplicate-keys?
I will be adding and removing entries very often after the data structure has been created so sorting the objects after the structure has been created would be very expensive.
回答1:
Both Apache Commons and Guava support multimaps, which are what you're looking for.
Alternatively, depending on your usecase, you could gather the elements in an ArrayList
and sort it afterwards in O(n lg n) total time.
Or, you could define a comparison that checks fitness first and other distinguishing properties of the items if the fitnesses compare equal.
回答2:
In classical computer science, the data structure you are looking for is called a Priority Queue.
It just so happens that since java 1.5, the standard java class library has an implementation: java.util.PriorityQueue.
I would use it.
回答3:
If you want to use a regular TreeMap
you could make it store value wrappers instead of the values themselves. These wrappers would then store multiple with the same key.
Otherwise you could use some kind of sorted list structure, based on a binary search tree. I wrote one a while ago which is available here: http://www.scottlogic.co.uk/2010/12/sorted_lists_in_java/ but I'm sure more standard implementations exist. The advantage of this kind of structure is that the contains(Object)
method runs in time logarithmic rather than linear time.
回答4:
If you only care about the order once all the individuals have been added, and don't add any new individual afterwards, then sorting an ArrayList is probably the fastest option.
Else, you may use a TreeSet, and have a comparator that compares by fitness first, and then by identity hash code if the fitnesses are equal (or by any other distinct property). All your objects will thus be different, but they will be sorted by fitness.
来源:https://stackoverflow.com/questions/8819550/efficiently-ordered-data-structure-that-supports-duplicate-keys