Java

Queue Interface – Java Collections

A queue is constructed in such a way that any additional elements are added to the end and removed from the beginning. The idea behind this is similar to the queues we encounter every day. For instance, when a new iPhone is released, we line up outside the Apple Store. Anyone who joins the line must stand at the end, and people are served according to the principle of FIFO (First In First Out). The person who receives the iPhone is then moved to the front of the line.

The Java collections language has two classes that implement the queue interface: LinkedList and PriorityQueue.
As a result of the fact that queue is an interface, we must instead create instances of LinkedList or PriorityQueue and assign them to the queue as follows:

Queue q1 = new LinkedList();
Queue q2 = new PriorityQueue();

Java Queue Example

import java.util.*;
public class QueueExample1 {
	 
   public static void main(String[] args) {
	  
      /*
       * We cannot create instance of a Queue as it is an
       * interface, we can create instance of LinkedList or
       * PriorityQueue and assign it to Queue
       */
      Queue<String> q = new LinkedList<String>();
	    
      //Adding elements to the Queue
      q.add("Rick");
      q.add("Maggie"); 
      q.add("Glenn");
      q.add("Negan");
      q.add("Daryl");
	    
      System.out.println("Elements in Queue:"+q);

      /*
       * We can remove element from Queue using remove() method,
       * this would remove the first element from the Queue 
       */
      System.out.println("Removed element: "+q.remove());
	    
      /*
       * element() method - this returns the head of the
       * Queue. Head is the first element of Queue
       */
      System.out.println("Head: "+q.element());
	    
      /*
       * poll() method - this removes and returns the 
       * head of the Queue. Returns null if the Queue is empty
       */
      System.out.println("poll(): "+q.poll());
	    
      /*
       * peek() method - it works same as element() method,
       * however it returns null if the Queue is empty
       */
      System.out.println("peek(): "+q.peek());
	    
      //Again displaying the elements of Queue
      System.out.println("Elements in Queue:"+q);
   }
}

Output:

Elements in Queue:[Rick, Maggie, Glenn, Negan, Daryl]
Removed element: Rick
Head: Maggie
poll(): Maggie
peek(): Glenn
Elements in Queue:[Glenn, Negan, Daryl]

Generics, which I’ve used in the example above, allow us to declare the type of the element we’re going to place into the collection. As you can see, I used Generics to specify the Queue’s type as String, making it only take items of the String type. Compilation errors will be generated if you attempt to add an element of a type that is not supported, adding safety and reducing the likelihood of errors in our application.

Methods of Queue interface

Boolean add(E e): Adds the requested element to the end of the queue. Returns true if the element is successfully added or false if it is unsuccessfully added, which essentially occurs when the queue reaches its maximum capacity and is no longer able to accept any more items.

E element(): This function returns the queue’s head (first element).

Boolean object offer: Similar to the add() function, this.

E remove(): This function returns the value after removing the head (first element) of the queue.

E poll(): This technique is somewhat similar to delete(). If the Queue is empty, the only circumstance in which poll() differs from remove() is that poll() returns null.

E peek(): This technique is very similar to the element() technique. The sole difference between peek() and element() is that, if the Queue is empty, the peek() function returns null.

Related Articles

Leave a Reply

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

Back to top button