java.util package.
package java.util;
public interface Queue<E> extends Collection
{
// Methods (abstract)
}
Queue Interface:
Queue interface:
| Sr. No. | Method | Description |
|---|---|---|
| 1 | boolean add(E e) |
Inserts the specified element into the queue.
If the queue is full, this method throws an IllegalStateException.
|
| 2 | boolean offer(E e) |
Inserts the specified element into the queue if possible.
Returns true on success and false if the queue is full.
|
| 3 | E remove() |
Retrieves and removes the head of the queue.
Throws NoSuchElementException if the queue is empty.
|
| 4 | E poll() |
Retrieves and removes the head of the queue, or returns null if the queue is empty.
|
| 5 | E element() |
Retrieves, but does not remove, the head of the queue.
Throws NoSuchElementException if the queue is empty.
|
| 6 | E peek() |
Retrieves, but does not remove, the head of the queue,
or returns null if the queue is empty.
|
Queue interface methods are designed to handle both normal and exceptional cases.
Methods like add() and remove() throw exceptions,
while offer() and poll() return special values (false or null).
Queue interface follows the FIFO (First-In-First-Out) order,
but some implementations like PriorityQueue order elements based on priority instead.
Queue implemented class i.e. LinkedList.
import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo
{
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<>();
// Adding elements
queue.add("Amit");
queue.add("Deepak");
queue.add("Kamal");
queue.add("Rahul");
queue.add("Ravi");
// Displaying the queue
System.out.println("Initial Queue: " + queue);
System.out.println("-------------------------");
// Accessing the head element
System.out.println("Head element (peek): " + queue.peek());
System.out.println("-------------------------");
// Removing elements
System.out.println("Removed element (poll): " + queue.poll());
System.out.println("Queue after poll: " + queue);
System.out.println("-------------------------");
// Adding another element using offer()
queue.offer("Mohit");
System.out.println("Queue after offer: " + queue);
System.out.println("-------------------------");
// Iterating through the queue
System.out.println("Iterating the Queue elements:");
for (String name : queue)
{
System.out.println(name);
}
}
}
Initial Queue: [Amit, Deepak, Kamal, Rahul, Ravi] ------------------------- Head element (peek): Amit ------------------------- Removed element (poll): Amit Queue after poll: [Deepak, Kamal, Rahul, Ravi] ------------------------- Queue after offer: [Deepak, Kamal, Rahul, Ravi, Mohit] ------------------------- Iterating the Queue elements: Deepak Kamal Rahul Ravi Mohit
Queue Interface:
List; elements are accessed only from the head or tail.
LinkedList),
but some implementations (like PriorityQueue) do not allow null elements.
PriorityQueue orders elements according to their priority instead of insertion sequence.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.