How to use Java Collections Queue in Java

The Queue interface is a subtype of java.util.Collection interface. This represents Ordered list of object where elements inserted at the end of the queue, and elements removed from the beginning of the queue. Now lets see some of the basic Queue operations.

Implementation

Since Queue is a interface,we need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Queue implementations of Java Collections.

  • java.util.LinkedList
  • java.util.PriorityQueue

Examples of creating Queue Instance

Queue queue1 = new LinkedList();
Queue queue2 = new PriorityQueue();

The order in which the elements are stored internally depends upon the type of implementation that we choose.

Adding elements to the queue

To add elements to the queue we will use the add() method to insert element. This throws “IllegalStateException” when it fails to add element due to capacity restriction.

Queue queue = new LinkedList();
queue.add("element1");
queue.add("element2");
queue.add("element3");

Other way of adding element to the queue is through offer() method. This method returns a boolean, if the insertion is successful it will return “true” else it will return “false”.

boolean insert = queue.offer("element4");

Accessing elements of the queue

Elements of the queue can be accessed in two ways.

Using element() method, This method returns the first element of the queue without removing from the queue. It throws “NoSuchElementException”  when the queue is empty.

queue.element()

Using peek() method, This method also returns the first element of the queue without removing from the queue. It throws “null”  when the queue is empty.

queue.peek()

Removing elements from the queue

Using remove() method, This method removes and returns the first element of the queue, will throw “NoSuchElementException”  when the queue is empty.

queue.remove()

Using poll() method, This method is also the same as the remove() method it removes and returns the first element of the queue and returns “null” when the queue is empty

queue.poll()

Code Snippet

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

public class QueueExample {

	public static void main(String[] args) {
	    //Creating a queue object through LinkedList
	    Queue queue = new LinkedList();
	    
	    System.out.println("Adding elements to the Queue" );
	    //Adding elements to the queue
	    queue.add("Java");
	    queue.add("jQuery");
	    queue.add("Hadoop");
	    
	    //Adding elements to the queue using offer() method
	    //offer()  method returns boolean to check if the value is added or not
	    boolean insert = queue.offer("HTML5");
	    System.out.println("Is HTML5 is inserted in the queue??"+insert);
	    
	    System.out.println( "Elements of the queue..." + queue );

	    System.out.println("Removing the element of the queue");
	    //The remove() method will remove the first element of the queue
	    System.out.println("Element Removed : " + queue.remove());
	    
	    //element() method will retrive a the current element of the queue,
	    //here we have removed Java so the next element will be jQuery.
	    System.out.println("Current Element: " + queue.element());
	    
	    //poll() method retrieves and removes the head of the queue and returns null if the queue is empty   
	    System.out.println("Remove the head element using poll(): " + queue.poll());
	    
	    //peek() method returns the current element in the queue and returns null if the queue is empty
	     System.out.println("Current element of the queue" + queue.peek()  );
	}
}

Output

Adding items to the Queue
Is HTML5 is inserted in the queue??true
Elements of the queue...[Java, jQuery, Hadoop, HTML5]
Removing the element of the queue
Element Removed : Java
Current Element: jQuery
Remove the head element using poll(): jQuery
Current element of the queue :Hadoop

Leave a Reply

Your email address will not be published. Required fields are marked *