GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Queue Interface In Java - GeeksforGeeks Queue Interface In JavaLast Updated : 1 Oct, 2026 The Queue interface is part of the java.util package and extends the Collection interface. It represents a data structure where elements are processed based on a specific order.
Output PriorityQueue elements: [10, 20, 40, 50, 30]
Declaration
We cannot instantiate a Queue directly as it is an interface. Instead, we use classes like PriorityQueue, LinkedList, or ArrayDeque that implement the Queue interface.
Hierarchy of Queue InterfaceIt extends the Collection interface and has implementations like LinkedList, ArrayDeque, PriorityQueue. ![]() Common Implementations of Queue Interface
Different Operations on Queue Interface using PriorityQueueDifferent operations on the Queue interface using PriorityQueue demonstrate how elements can be added, removed, and accessed while maintaining a priority-based ordering instead of simple FIFO. 1. Adding ElementsTo add an element in a queue, we can use the add() method. The insertion order is not retained in the PriorityQueue. The elements are stored based on the priority order which is ascending by default.
Output [For, Geeks, Geeks] Explanation: The add() method inserts an element into the queue. In a PriorityQueue, elements are ordered according to their priority, not insertion order. 2. Removing ElementsThe Queue interface provides remove() and poll() to retrieve and remove the head of the queue. The main difference between them is their behavior when the queue is empty.
Output Initial Queue: [For, Geeks, Geeks] After Remove: [For, Geeks] Poll Method: For Final Queue: [Geeks] Explanation: The remove() method removes a specified element, while poll() removes and returns the head of the queue. remove() throws an exception if the queue is empty, whereas poll() returns null. 3. Accessing ElementsWe can access the head element without removing it using peek() or element().
Output Head using peek(): For Head using element(): For Queue after accessing head: [For, Geeks, Geeks] Explanation: The peek() and element() methods retrieve the head element without removing it. peek() returns null if the queue is empty, while element() throws an exception. 4. Iterating the QueueThere are multiple ways to iterate through the Queue. The most famous way is converting the queue to the array and traversing using the for loop. The queue has also an inbuilt iterator which can be used to iterate through the queue.
Output For Geeks Geeks Explanation: A queue can be traversed using an Iterator. For a PriorityQueue, iteration does not necessarily follow priority order; to process elements according to priority, repeatedly use poll(). Methods of Queue Interface
Advantages of Queue Interface
Comment Explore | ||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |