Java का PriorityQueue
In this page:
PriorityQueue<Type> queue = new PriorityQueue<>();
queue.offer(item);
queue.poll(); // smallest first
queue.peek();
Introduction to PriorityQueue
एक PriorityQueue एक queue implementation है जहां elements साधारण first-in-first-out order के बजाय priority के आधार पर dequeue होते हैं। Default रूप से यह एक min-heap की तरह व्यवहार करता है, हमेशा सबसे छोटे element (natural ordering से) को head पर रखते हुए, पहले poll होने के लिए तैयार।
उदाहरण: Introduction to PriorityQueue
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5); pq.add(1); pq.add(3);
System.out.println(pq.poll()); // smallest first: 1
}
}
Login to try C/C++/Java/PHP code in the editor
Creating a Max-Heap Queue
आप किसी PriorityQueue को एक max-heap में बदल सकते हैं, जहां सबसे बड़ा element सबसे छोटे की जगह head पर बैठता है, उसके constructor में Collections.reverseOrder(), या एक समान custom reverse comparator पास करके।
उदाहरण: Creating a Max-Heap Queue
import java.util.PriorityQueue;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
maxHeap.add(5); maxHeap.add(1); maxHeap.add(3);
System.out.println(maxHeap.poll()); // largest first: 5
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Objects in PriorityQueue
आप किसी PriorityQueue के अंदर custom objects तब तक store कर सकते हैं जब तक class Comparable implement करती हो, इसका natural order define करते हुए, या आप queue के constructor में एक custom Comparator पास करें जो उसे बताए कि उन objects को कैसे rank करना है।
उदाहरण: Custom Objects in PriorityQueue
// Import java.util.PriorityQueue so it can be used by its short name
import java.util.PriorityQueue;
// Import java.util.Comparator so it can be used by its short name
import java.util.Comparator;
// Define the class Main
public class Main {
// Define the record Task (an immutable data carrier)
record Task(String name, int priority) {}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new PriorityQueue object and store it in pq
PriorityQueue<Task> pq = new PriorityQueue<>(Comparator.comparingInt(Task::priority));
pq.add(new Task("Low", 3));
pq.add(new Task("High", 1));
// Print a line to the console
System.out.println(pq.poll());
}
}
Login to try C/C++/Java/PHP code in the editor
Queue Maintenance and Traversal
आप किसी PriorityQueue का current size जांच सकते हैं, यह test कर सकते हैं कि यह खाली है या नहीं, या इसके अंदर कहीं से भी एक specific element खोज और हटा सकते हैं, हालांकि किसी arbitrary (non-head) element को हटाना एक linear-time operation है क्योंकि internal heap array पूरी तरह sorted नहीं होती।
उदाहरण: Queue Maintenance and Traversal
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5); pq.add(1); pq.add(3);
System.out.println(pq.size() + " " + pq.isEmpty());
pq.remove(3); // linear-time, not the head
System.out.println(pq);
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Sorting Simulator
एक PriorityQueue उन datasets को manage करने के लिए खासतौर पर उपयोगी है जो समय के साथ dynamically update होते हैं, जैसे कोई task scheduler या एक चल रहा top-K tracker, क्योंकि यह हमेशा सबसे ऊंची (या सबसे नीची) priority वाले element को पूरे collection को फिर से sort किए बिना तुरंत accessible रखता है।
उदाहरण: Dynamic Sorting Simulator
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> topScores = new PriorityQueue<>();
int[] incoming = {50, 90, 20, 70};
for (int score : incoming) {
topScores.add(score);
if (topScores.size() > 2) topScores.poll(); // keep top 2
}
System.out.println(topScores);
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
PriorityQueueको print या iterate करना और sorted output की उम्मीद करना, जबकि सिर्फ head के सबसे छोटे होने की गारंटी है और बाकी heap order में है। - default रूप से max-heap की उम्मीद करना, जबकि यह min-heap है; सबसे बड़ा पहले पाने के लिए
Collections.reverseOrder()पास करें। - बिना Comparator के ऐसे objects जोड़ना जो
Comparableनहीं हैं, जोClassCastExceptionफेंकता है।
TreeMap,TreeSet, औरLinkedHashMapsorted या insertion-ordered collections प्रदान करते हैं।ComparableऔरComparatordefine करते हैं कि objects को कैसे compare और sort किया जाए।PriorityQueueelements को priority के अनुसार process करता है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: