priority-queue

Does PriorityQueue heapify itself if any object in the collection is mutated so that it's key(used in comparator) changes?

时间秒杀一切 提交于 2019-12-02 06:25:49
问题 I just want to know if Java's PriorityQueue collection automatically heapifies, if any of the keys used in the comparator is mutated, or do i need to explicitly call heapify, to order the binary tree ? I am experimenting with the data structure to use it in a cache like setting, where an object in the collection should be immediately moved to the head, as soon as it is referenced. 回答1: The queue automatically heapifies on operations that mutates the queue itself, namely offer , poll , remove

Does a PriorityQueue allow elements already within the queue to be reordered?

人走茶凉 提交于 2019-12-02 04:24:22
I want to augment or lower the priority of items in a PriorityQueue : for example, I might be downloading a long list of images and suddenly want the thirtieth one to have highest priority. As I understand it, poll() always returns the queue object with the lowest value (as determined by a comparator). If I can lower the value of an item already in a queue (e.g. if this value is determined by an int in the object and I reduce the int value in some other function), will it be returned first by poll() , or is the sorting that allows poll() to do this done at insert time (e.g. by bubbling new

Why does my PriorityBlockingQueue in Java not sort properly?

本小妞迷上赌 提交于 2019-12-02 04:14:26
For some reason when I add to the priority queue, it doesn't sort my strings entirely alphabetically and I can't see why. This is the code which adds to the PriorityBlockingQueue: String toAdd = String.format("%s/%s", directory, s); outputData.add(toAdd); But I get not entirely sorted output (only first few lines but you can see it's not sorted): ../StartingTree/files/abknl/apfmpohgyh/a.class ../StartingTree/files/abknl/apfmpohgyh/a.java ../StartingTree/files/abknl/aqybc/aeph.java ../StartingTree/files/abknl/apfmpohgyh/bnjuxxdi.class ../StartingTree/files/abknl/bbxudleuf/jlffhq/y/xwjj

Java: strange order of queue made from priority queue

 ̄綄美尐妖づ 提交于 2019-12-02 04:04:26
I wrote a maze solving program which is supposed to support DFS, BFS, A*, Dijkstra's, and greedy algorithm. Anyway, I chose PriorityQueue for my frontier data structure since I thought a priority can behave like a queue, stack, or priority queue depends on the implementation of the comparator. This is how I implemented my comparator to turn the priority queue into a queue: / Since the "natural ordering" of a priority queue has the least element at the head and a conventional comparator returns -1 when the first is less than the second, the hacked comparator always return 1 so that the current

Does PriorityQueue heapify itself if any object in the collection is mutated so that it's key(used in comparator) changes?

会有一股神秘感。 提交于 2019-12-02 00:49:30
I just want to know if Java's PriorityQueue collection automatically heapifies, if any of the keys used in the comparator is mutated, or do i need to explicitly call heapify, to order the binary tree ? I am experimenting with the data structure to use it in a cache like setting, where an object in the collection should be immediately moved to the head, as soon as it is referenced. The queue automatically heapifies on operations that mutates the queue itself, namely offer , poll , remove and add . If you mutate an element you have to remove and re-insert it . The priority queue has no way of

c++ ordered(stable) priority queue

感情迁移 提交于 2019-12-02 00:35:58
问题 I am implementing a toy scheduler which reads a input file of process specifications such as arrival time, total run time and then schedules the process based on random io/cpu bursts. The file is of the format Arrival time, total CPU time, CPU burst, IO Burst. Now, when there are two process with same arrival time, the scheduler have to schedule the process first the process which is mentioned first in the file. I am keeping the entries in the file in a priority queue. struct EventComparator{

Queue not ordering naturally [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-01 21:33:24
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Why is this strange order happens in PriorityQueue in java? Please, see the code below: public static void main(String[] args) { Queue<String> q = new PriorityQueue<String>(); q.offer("car"); q.offer("airplane"); q.offer("bicycle"); Iterator<String> i = q.iterator(); while(i.hasNext()) System.out.print(i.next() + " "); } Can someone explain me why the output is airplane car bicycle instead of airplane bicycle

Java: PriorityQueue initializations

别来无恙 提交于 2019-12-01 21:16:38
I am trying to understand the following line which initiates a Priority Queue: PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[1] - a[1]); Comparing with Constructor section in the document, https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html I couldn't figure out which Constructor it uses. Could someone please share the thought? Also, is there a document that could better explain/define syntax (a, b) -> b[1] - a[1] ... though I could guess what it means. Thanks a lot! Your construction of the PriorityQueue uses a constructor that didn't yet exist in 1.7, which is

Queue not ordering naturally [duplicate]

青春壹個敷衍的年華 提交于 2019-12-01 19:01:48
Possible Duplicate: Why is this strange order happens in PriorityQueue in java? Please, see the code below: public static void main(String[] args) { Queue<String> q = new PriorityQueue<String>(); q.offer("car"); q.offer("airplane"); q.offer("bicycle"); Iterator<String> i = q.iterator(); while(i.hasNext()) System.out.print(i.next() + " "); } Can someone explain me why the output is airplane car bicycle instead of airplane bicycle car ? Since in the API it says that the elements of the priority queue are ordered according to their natural ordering. PriorityQueue is based on a priority heap. This

How is the Java priority Queue supposed to work? [duplicate]

。_饼干妹妹 提交于 2019-12-01 18:15:02
This question already has an answer here: PriorityQueue.toString wrong element order 4 answers Short story, I'm implementing a graph and now I'm working on the Kruskal, I need a priority queue. My definition of a priority queue is that the element with the smallest key would come first? Is this wrong? Because when I insert the weighted edges(or numbers) in the queue they don't end up sorted. PriorityQueue<Integer> tja = new PriorityQueue<Integer>(); tja.add(55); tja.add(99); tja.add(1); tja.add(102); tja.add(54); tja.add(51); System.out.println(tja); That would print out this; [1, 54, 51, 102,