← Back to Advanced Java Course | Chapter 5: Collections Advanced | Lesson 4 of 4

Java का PriorityQueue

एक PriorityQueue एक ऐसी line है जहां सबसे ज़रूरी item हमेशा पहले जाता है, जैसे एक emergency room जो सबसे बीमार मरीज़ को पहले treat करती है। यह पहले आओ, पहले पाओ नहीं है।
Syntax
java
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

java
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
	}
}

Creating a Max-Heap Queue

आप किसी PriorityQueue को एक max-heap में बदल सकते हैं, जहां सबसे बड़ा element सबसे छोटे की जगह head पर बैठता है, उसके constructor में Collections.reverseOrder(), या एक समान custom reverse comparator पास करके।

उदाहरण: Creating a Max-Heap Queue

java
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
	}
}

Custom Objects in PriorityQueue

आप किसी PriorityQueue के अंदर custom objects तब तक store कर सकते हैं जब तक class Comparable implement करती हो, इसका natural order define करते हुए, या आप queue के constructor में एक custom Comparator पास करें जो उसे बताए कि उन objects को कैसे rank करना है।

उदाहरण: Custom Objects in PriorityQueue

java
// 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());
	}
}

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

java
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);
	}
}

Dynamic Sorting Simulator

एक PriorityQueue उन datasets को manage करने के लिए खासतौर पर उपयोगी है जो समय के साथ dynamically update होते हैं, जैसे कोई task scheduler या एक चल रहा top-K tracker, क्योंकि यह हमेशा सबसे ऊंची (या सबसे नीची) priority वाले element को पूरे collection को फिर से sort किए बिना तुरंत accessible रखता है।

उदाहरण: Dynamic Sorting Simulator

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी PriorityQueue को print या iterate करना और sorted output की उम्मीद करना, जबकि सिर्फ head के सबसे छोटे होने की गारंटी है और बाकी heap order में है।
  2. default रूप से max-heap की उम्मीद करना, जबकि यह min-heap है; सबसे बड़ा पहले पाने के लिए Collections.reverseOrder() पास करें।
  3. बिना Comparator के ऐसे objects जोड़ना जो Comparable नहीं हैं, जो ClassCastException फेंकता है।
चैप्टर सारांश
  • TreeMap, TreeSet, और LinkedHashMap sorted या insertion-ordered collections प्रदान करते हैं।
  • Comparable और Comparator define करते हैं कि objects को कैसे compare और sort किया जाए।
  • PriorityQueue elements को priority के अनुसार process करता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.