![]() |
VOOZH | about |
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.
public interface Queue<E> extends Collection<E>
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.
Queue<Obj> queue = new LinkedList<Obj>();
or
Queue<Obj> queue = new PriorityQueue<>();
PriorityQueue elements: [10, 20, 40, 50, 30]
Note:PriorityQueue arranges elements according to priority order (ascending by default), not insertion order.
It extends the Collection interface and has implementations like LinkedList, ArrayDeque, PriorityQueue.
Different 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.
To 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.
[For, Geeks, Geeks]
To remove an element from a queue, we can use the remove() method. If there are multiple objects, then the first occurrence of the object is removed. The poll() method is also used to remove the head and return it.
Initial Queue: [For, Geeks, Geeks] After Remove: [For, Geeks] Poll Method: For Final Queue: [Geeks]
We can access the head element without removing it using peek() or element().
Head using peek(): For Head using element(): For Queue after accessing head: [For, Geeks, Geeks]
There 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.
For Geeks Geeks
| Method | Description |
|---|---|
| add(E e) | Inserts the specified element; throws exception if insertion fails. |
| offer(E e) | Inserts the specified element; returns false if insertion fails. |
| remove() | Removes and returns the head of the queue; throws exception if empty. |
| poll() | Removes and returns the head; returns null if empty. |
| peek() | Retrieves, but does not remove, the head; returns null if empty. |
| size() | Returns the number of elements in the queue. |
| isEmpty() | Returns true if the queue contains no elements. |
| contains(Object o) | Returns true if the queue contains the specified element. |
| iterator() | Returns an iterator over the elements in the queue. |
| toArray() | Converts the queue elements into an array. |
| addFirst(E e) | Inserts element at the front (Deque only). |
| addLast(E e) | Inserts element at the end (Deque only). |
| offerFirst(E e) | Inserts element at the front; returns false if fails (Deque only). |
| offerLast(E e) | Inserts element at the end; returns false if fails (Deque only). |
| removeFirst() | Removes and returns the first element (Deque only). |
| removeLast() | Removes and returns the last element (Deque only). |
| pollFirst() | Removes and returns the first element; returns null if empty (Deque only). |
| pollLast() | Removes and returns the last element; returns null if empty (Deque only). |
| getFirst() | Retrieves, but does not remove, the first element (Deque only). |
| getLast() | Retrieves, but does not remove, the last element (Deque only). |
| peekFirst() | Retrieves, but does not remove, the first element; returns null if empty (Deque only). |
| peekLast() | Retrieves, but does not remove, the last element; returns null if empty (Deque only). |
| put(E e) | Inserts element, waits if necessary (BlockingQueue only). |
| take() | Removes and returns head element, waits if empty (BlockingQueue only). |