PriorityQueue has objects with the same priority

后端 未结 2 873
渐次进展
渐次进展 2020-12-16 00:59

I\'m using a priority queue to sort and use a large number of custom objects. The objects have a \"weight\" that is their natural ordering. However, different objects that a

相关标签:
2条回答
  • 2020-12-16 01:29

    If you need to have an ordering according the insertion order you need to use an extra element for timestamp. I.e. on insertions and equal weight use timestamp to see which element was inserted first. So CustomObject should be something like:

    class CustomObject {  
       int weight;  
       long timestamp;  
    }
    

    And the comparison should be:

    public int compareTo (CustomObject o) {  
        int thisWeight = this.weight;  
        int thatWeight = o.weight;  
        if (thisWeight != thatWeight) {  
            return thisWeight - thatWeight;  
        }  
        else {  
            return this.timestamp - o.timestamp;  
        }  
    }  
    

    The smaller timestamp means it was inserted earlier so you keep in the insertion order.

    You could also use a "logical" time by maintaining a counter that you update on each add or remove.

    0 讨论(0)
  • 2020-12-16 01:52

    You could use an automatically-incremented sequence number as a secondary key, and use it to break ties.

    Javadoc for PriorityBlockingQueue includes an example of this technique:

    Operations on this class make no guarantees about the ordering of elements with equal priority. If you need to enforce an ordering, you can define custom classes or comparators that use a secondary key to break ties in primary priority values. For example, here is a class that applies first-in-first-out tie-breaking to comparable elements. To use it, you would insert a new FIFOEntry(anEntry) instead of a plain entry object.

    class FIFOEntry<E extends Comparable<? super E>>
         implements Comparable<FIFOEntry<E>> {
       final static AtomicLong seq = new AtomicLong();
       final long seqNum;
       final E entry;
       public FIFOEntry(E entry) {
         seqNum = seq.getAndIncrement();
         this.entry = entry;
       }
       public E getEntry() { return entry; }
       public int compareTo(FIFOEntry<E> other) {
         int res = entry.compareTo(other.entry);
         if (res == 0 && other.entry != this.entry)
           res = (seqNum < other.seqNum ? -1 : 1);
         return res;
       }
     }
    
    0 讨论(0)
提交回复
热议问题