Working with Java Queues and PriorityQueue

📘 Java 👁 53 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

A Queue in Java is a collection used to hold elements prior to processing. It follows FIFO (First-In-First-Out) order by default, while some implementations like PriorityQueue allow custom ordering.


1. Queue Interface

  • Part of the java.util package.

  • Extends Collection interface.

  • Common operations:

MethodDescription
add(e)Inserts an element (throws exception if full)
offer(e)Inserts an element (returns false if full)
remove()Removes and returns head (throws exception if empty)
poll()Removes and returns head (returns null if empty)
element()Returns head without removing (throws exception if empty)
peek()Returns head without removing (returns null if empty)

Example using LinkedList as Queue

import java.util.LinkedList; import java.util.Queue; public class Test { public static void main(String[] args) { Queue queue = new LinkedList<>(); queue.add("Java"); queue.add("Python"); queue.offer("C++"); System.out.println("Queue: " + queue); System.out.println("Head of queue: " + queue.peek()); queue.remove(); System.out.println("After removal: " + queue); } }

Output:

Queue: [Java, Python, C++] Head of queue: Java After removal: [Python, C++]

2. PriorityQueue

  • PriorityQueue is a queue that orders elements according to their natural order or a custom comparator.

  • Does not allow null elements.

  • Head element is always the smallest/largest depending on ordering.

Features

  • Implements Queue interface.

  • Allows efficient access to the element with highest priority.

  • Internally implemented as a min-heap.

Example:

import java.util.PriorityQueue; public class Test { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue<>(); pq.add(30); pq.add(10); pq.add(20); System.out.println("PriorityQueue: " + pq); System.out.println("Head of queue: " + pq.peek()); pq.poll(); System.out.println("After removal: " + pq); } }

Output:

PriorityQueue: [10, 30, 20] Head of queue: 10 After removal: [20, 30]

3. Key Differences: Queue vs PriorityQueue

FeatureQueue (LinkedList)PriorityQueue
OrderFIFO (insertion order)Natural order or custom comparator
Null elementsAllowedNot allowed
Head elementFirst inserted elementElement with highest priority
ImplementationLinkedListMin-heap
Use CaseTask scheduling, BFSPriority-based scheduling

4. Key Points

  • Queue → General FIFO behavior.

  • PriorityQueue → Custom ordering based on priority.

  • Both implement Queue interface.

  • Use offer/poll/peek methods for safe operations.

  • PriorityQueue is not thread-safe; use PriorityBlockingQueue for concurrent scenarios.


5. Conclusion

Java Queues provide efficient data handling in FIFO order, while PriorityQueue enables priority-based processing. Choosing the right implementation ensures optimal performance and correct task ordering in Java applications.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes