priority-queue

How do I use a PriorityQueue?

不羁的心 提交于 2019-11-26 01:22:28
问题 How do I get a PriorityQueue to sort on what I want it to sort on? Also, is there a difference between the offer and add methods? 回答1: Use the constructor overload which takes a Comparator<? super E> comparator and pass in a comparator which compares in the appropriate way for your sort order. If you give an example of how you want to sort, we can provide some sample code to implement the comparator if you're not sure. (It's pretty straightforward though.) As has been said elsewhere: offer

Updating Java PriorityQueue when its elements change priority

∥☆過路亽.° 提交于 2019-11-26 01:07:26
问题 I\'m trying to use a PriorityQueue to order objects using a Comparator . This can be achieved easily, but the objects class variables (with which the comparator calculates priority) may change after the initial insertion. Most people have suggested the simple solution of removing the object, updating the values and reinserting it again, as this is when the priority queue\'s comparator is put into action. Is there a better way other than just creating a wrapper class around the PriorityQueue

The built-in iterator for java&#39;s PriorityQueue does not traverse the data structure in any particular order. Why?

心已入冬 提交于 2019-11-26 00:20:02
问题 This is straight from the Java Docs: This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()). So basically, my PriorityQueue works fine, but printing it out to the screen using its own built in toString() method caused me to see this

Priority queue in .Net [closed]

梦想与她 提交于 2019-11-25 23:39:20
问题 I am looking for a .NET implementation of a priority queue or heap data structure Priority queues are data structures that provide more flexibility than simple sorting, because they allow new elements to enter a system at arbitrary intervals. It is much more cost-effective to insert a new job into a priority queue than to re-sort everything on each such arrival. The basic priority queue supports three primary operations: Insert(Q,x). Given an item x with key k, insert it into the priority