Home » Java Queue and PriorityQueue

Java Queue and PriorityQueue

by Online Tutorials Library

Java Queue Interface

The interface Queue is available in the java.util package and does extend the Collection interface. It is used to keep the elements that are processed in the First In First Out (FIFO) manner. 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

Methods of Java Queue Interface

Method Description
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.

Java PriorityQueue Example

FileName: TestCollection12.java

Test it Now

Output:

head:Amit         head:Amit         iterating the queue elements:         Amit         Jai         Karan         Vijay         Rahul         after removing two elements:         Karan         Rahul         Vijay  

Java PriorityQueue Example: Book

Let’s see a PriorityQueue example where we are adding books to queue and printing all the books. The elements in PriorityQueue must be of Comparable type. String and Wrapper classes are Comparable by default. To add user-defined objects in PriorityQueue, you need to implement Comparable interface.

FileName: LinkedListExample.java

Output:

Traversing the queue elements:  101 Data Communications & Networking Forouzan Mc Graw Hill 4  233 Operating System Galvin Wiley 6  121 Let us C Yashwant Kanetkar BPB 8  After removing one book record:  121 Let us C Yashwant Kanetkar BPB 8  233 Operating System Galvin Wiley 6  

You may also like