Queue In Java

Queue Interface

The interface Queue is available in the java.util package and does extend the Collection interface. Queue follows the First In First Out (FIFO) order. It is an ordered list of objects, where insertion of elements occurs at the end of the list, and removal of elements occur at the beginning of the list.

Being an interface, the queue requires, for the declaration, a concrete class, and the most common classes are the LinkedList and PriorityQueue in Java. Implementations done by these classes are not thread safe. If it is required to have a thread safe implementation, PriorityBlockingQueue is an available option.

Queue Interface Declaration

      public interface Queue<E> extends Collection<E>

Methods of Java Queue Interface

MethodDescription
boolean add(object)It is used to insert the specified element into this queue and return true upon success.
boolean offer(object)It is used to insert the specified element into this queue.
Object remove()It is used to retrieves and removes the head of this queue.
Object poll()It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element()It is used to retrieves, but does not remove, the head of this queue.
Object peek()It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Features of a Queue

The following are some important features of a queue.

  • As discussed earlier, FIFO concept is used for insertion and deletion of elements from a queue.
  • The Java Queue provides support for all of the methods of the Collection interface including deletion, insertion, etc.
  • PriorityQueue, ArrayBlockingQueue and LinkedList are the implementations that are used most frequently.
  • The NullPointerException is raised, if any null operation is done on the BlockingQueues.
  • Those Queues that are present in the util package are known as Unbounded Queues.
  • Those Queues that are present in the util.concurrent package are known as bounded Queues.
  • All Queues barring the Deques facilitates removal and insertion at the head and tail of the queue; respectively. In fact, deques support element insertion and removal at both ends.

PriorityQueue Class

PriorityQueue is also class that is defined in the collection framework that gives us a way for processing the objects on the basis of priority. It is already described that the insertion and deletion of objects follows FIFO pattern in the Java queue. However, sometimes the elements of the queue are needed to be processed according to the priority, that’s where a PriorityQueue comes into action.

 

PriorityQueue

Class Declaration

Let’s see the declaration for java.util.PriorityQueue class.

   public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !