priority-queue

java heapify method using priorityQueue

不羁岁月 提交于 2019-12-08 18:59:26
I used priorityQueue as a max-heap implementation in my java program. Right now I need to heapify the created heap for calculating the maximum value. It seems that priorityQueue does not implement heapify method. So my question would be is there anyway for handling this problem using priorityQueue? If no, is there any reliable implementation for Max-heap in java that has heapify method? Note that my program use its own comparator. So this implementation should support that. Some more explanation: PriorityQueue<Customer> marginalGainHeap = new PriorityQueue<Customer>( 1, new Comparator<Customer

Add Key and Value into an Priority Queue and Sort by Key in Java

爷,独闯天下 提交于 2019-12-08 18:23:04
问题 I am trying to take in a List of strings and add them into a Priority Queue with Key and Value. The Key being the word and the value being the string value of the word. Then I need to sort the queue with the highest string value first. The priority queue is not letting me add 2 values. public static List<String> pQSortStrings(List<String> strings) { PriorityQueue<String, Integer> q = new PriorityQueue<>(); for (int x = 0; x < strings.size(); x++) { q.add(strings.get(x),calculateStringValue

How to get a non-const top element from priority_queue with user-defined objects?

南笙酒味 提交于 2019-12-08 15:11:15
问题 std::priority_queue::top returns a constant value. However, I would like to remove the top element from the priority queue and be able to modify it somewhere else. priority_queue<SomeClass, vector<SomeClass>, SomeClassCompare > pQueue; ... SomeClass *toBeModified = &(pQueue.top()); pQueue.pop(); toBeModified->setMember(3); // I would like to do this Is there a way I can grab the top element from a priority queue (and remove from the queue) and modify it as I wish? 回答1: Standard containers and

java heapify method using priorityQueue

依然范特西╮ 提交于 2019-12-08 08:06:19
问题 I used priorityQueue as a max-heap implementation in my java program. Right now I need to heapify the created heap for calculating the maximum value. It seems that priorityQueue does not implement heapify method. So my question would be is there anyway for handling this problem using priorityQueue? If no, is there any reliable implementation for Max-heap in java that has heapify method? Note that my program use its own comparator. So this implementation should support that. Some more

Does Hazelcast have a distributed priority queue implementation?

别说谁变了你拦得住时间么 提交于 2019-12-08 03:33:24
问题 We need to implement a distributed priority queue within Hazelcast at the moment and the only documentation we can find on the matter is this: https://blog.hazelcast.com/priority-queueing-using-spi/, which is not a production ready implementation. It references MigrationAwareService and BackupAwareOperation interfaces and provides a basic example for how to implement them, but after inspecting Hazelcast's own QueueService it doesn't look like these examples will suffice. Is it better to use

Perfect powers of numbers which can fit in 64 bit size integer (using priority queues)

纵饮孤独 提交于 2019-12-07 11:14:54
问题 How can we print out all perfect powers that can be represented as 64-bit long integers: 4, 8, 9, 16, 25, 27, .... A perfect power is a number that can be written as ab for integers a and b ≥ 2. It's not a homework problem, I found it in job interview questions section of an algorithm design book. Hint, the chapter was based on priority queues. Most of the ideas I have are quadratic in nature, that keep finding powers until they stop fitting 64 bit but that's not what an interviewer will look

Java PriorityQueue Comparator - How/When do you sort?

泪湿孤枕 提交于 2019-12-07 09:18:30
问题 I'm initialising a Priority Queue like: strategy = new FuelPriority(); incoming = new PriorityQueue<Vehicle>(1, strategy); The code for my Comparator class is: public class FuelPriority implements Comparator<Object> { public int compare(Object o1, Object o2) { Vehicle a1 = (Vehicle) o1; Vehicle a2 = (Vheicle) o2; return Integer.compare(a1.getFuelLevel(), a2.getFuelLevel()); } } After running a simulation, the elements aren't ordered at all - they are random; I set a breakpoint in the compare

how to Update a key in Priority Queue in O(log n ) time in dijkstra's algorithm?

耗尽温柔 提交于 2019-12-07 05:00:30
问题 I have been working on dijkstra's algorithm for the past one week one I have the correct running code for it in java. It is using array for the computation of standard findMin function which gives you the vertex with smallest distance.Obviously it is O(n) and Now I am looking to implement it using Priority Queue(Min Heaps) What My thought process is: while (there are unseen Vertex) { vertex= get TheVertex WithSmallest Distance Yet;//(In can be done in O(log n) using heap) for this vertex {

Schedule multiple async task in android

做~自己de王妃 提交于 2019-12-07 02:23:10
问题 I want to schedule or queue to execute Multiple AsyncTask in background. I have a AsyncTask for HTTP post request that is running in Service, At the same time i make a one more HTTP request in UI thread of AsyncTask. UI thread taking too long time to execute, because already one thread is running in service. How to resolve this problem.? I need to Pause a service thread and I want to execute UI thread first and then restart a service thread again for AsyncTask. Thanks in advance 回答1: As

Strange ordering in PriorityQueue in Java [duplicate]

跟風遠走 提交于 2019-12-06 11:33:26
This question already has answers here : The built-in iterator for java's PriorityQueue does not traverse the data structure in any particular order. Why? (5 answers) Closed 3 years ago . I am trying to use priority queue to keep an ordered list of integers. in a simple exaple like this: PriorityQueue<Integer> queue = new PriorityQueue<>(); queue.offer(3000); queue.offer(1999); queue.offer(999); for(Integer i : queue) System.out.println(i); This prints 999 3000 1999 This is not what I am expecting considering natural odering. I simply want to iterate without removing or adding through the