Simple queues are represented by an instance of the Queue interface.
A queue always has an entry point and an exit point for its elements.
The exit point is called the head and the entry point is called the tail.
A queue lets you perform three basic operations:
The Queue interface defines two methods for each of the three operations.
One method throws an exception if the operation is not possible; the other method returns a value (false or null) to indicate the failure.
The Queue interface adds six methods to provide the functionality of a FIFO queue.
Category | Method | Description |
---|---|---|
Adding an element to the queue | boolean add(E e) | Adds an element to the queue if it is possible. Otherwise, it throws an exception. |
Adding an element to the queue | boolean offer(E e) | Adds an element to the queue without throwing an exception if the element cannot not be added. It returns false on failure and true on success. |
Removing an element from the queue | E remove() | Retrieves and removes the head of the queue. It throws an exception if the queue is empty. |
Removing an element from the queue | E poll() | Performs the same job as the remove() method. However, it returns null if the queue is empty instead of throwing an exception. |
Peeking at the head of the queue | E element() | Retrieves the head of the queue without removing it from the queue. It throws an exception if the queue is empty. |
Peeking at the head of the queue | E peek() | Performs the same job as the element() method. However, it returns null if the queue is empty instead of throwing an exception. |
LinkedList and PriorityQueue are two implementation classes for the Queue interface.
The following code uses a LinkedList as a FIFO queue.
An instance of the LinkedList class can be used as a FIFO queue or a LIFO queue.
import java.util.LinkedList; import java.util.NoSuchElementException; import java.util.Queue; public class Main { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.add("XML"); // offer() will work the same as add() queue.offer("Javascript"); queue.offer("Json"); queue.offer("Java"); System.out.println("Queue: " + queue); // Let's remove elements until the queuee is empty while (queue.peek() != null) { System.out.println("Head Element: " + queue.peek()); queue.remove();/* ww w . j a v a2s. co m*/ System.out.println("Removed one element from Queue"); System.out.println("Queue: " + queue); } // Now Queue is empty. Try calling the peek(), // element(), poll() and remove() methods System.out.println("queue.isEmpty(): " + queue.isEmpty()); System.out.println("queue.peek(): " + queue.peek()); System.out.println("queue.poll(): " + queue.poll()); try { String str = queue.element(); System.out.println("queue.element(): " + str); } catch (NoSuchElementException e) { System.out.println("queue.element(): Queue is empty."); } try { String str = queue.remove(); System.out.println("queue.remove(): " + str); } catch (NoSuchElementException e) { System.out.println("queue.remove(): Queue is empty."); } } }