Java provides us Queue interface, where we can keep and handle elements before processing. Except the methods that Collection provides, it also supports some basic operations in order to simulate the classic queue structure. Each of these operations exists in two forms:
1. if a method fails, an exception is thrown. This form includes add(), remove() and element() methods.
2. if a method fails, a special value is returned (null or false). This form contains offer(), poll() and peek() operations.
The interface Queue is defined as below
1 2 3 4 5 6 7 8 9 |
public interface Queue<E> extends Collection<E> { E element(); boolean offer(E e); E peek(); E poll(); E remove(); } |
[Java Doc]
The add method, which Queue inherits from Collection, inserts an element unless it would violate the queue’s capacity restrictions, in which case it throws IllegalStateException. The offer method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returning false.
The remove and poll methods both remove and return the head of the queue. Exactly which element gets removed is a function of the queue’s ordering policy. The remove and poll methods differ in their behavior only when the queue is empty. Under these circumstances, remove throws NoSuchElementException, while poll returns null.
The element and peek methods return, but do not remove, the head of the queue. They differ from one another in precisely the same fashion as remove and poll: If the queue is empty, element throws NoSuchElementException, while peek returns null.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package net.tecbar.collection; import java.util.LinkedList; import java.util.Queue; public class QueueDemo1 { public static void main(String[] args) { System.out.println("Adding items to the Queue"); final Queue<String> queue = new LinkedList<String>(); //add element queue.add("Red"); queue.add("Black"); queue.add("White"); System.out.println("Items in the queue..." + queue); System.out.println("============================"); //remove the 1st element System.out.println("queue.remove() removes element: " + queue.remove()); System.out.println("Items in the queue..." + queue); System.out.println("============================"); // retrieve element System.out.println("queue.element() retrieves element: " + queue.element()); System.out.println("Items in the queue..." + queue); System.out.println("============================"); //poll() method retrieves and removes the head of this queue or return null if this queue is empty System.out.println("queue.poll() removes and retrieves element, null if empty: " + queue.poll()); System.out.println("Items in the queue..." + queue); System.out.println("============================"); //peek() just returns the current element in the queue, null if empty System.out.println("queue.peek() retrieves element, null is empty " + queue.peek()); System.out.println("Items in the queue..." + queue); System.out.println("============================"); } } |
output
Adding items to the Queue
Items in the queue...[Red, Black, White]
============================
queue.remove() removes element: Red
Items in the queue...[Black, White]
============================
queue.element() retrieves element: Black
Items in the queue...[Black, White]
============================
queue.poll() removes and retrieves element, null if empty: Black
Items in the queue...[White]
============================
queue.peek() retrieves element, null is empty White
Items in the queue...[White]
============================