Java Queue

A queue is another kind of linear data structure that is used to store elements just like any other data structure but in a particular manner. In simple words, we can say that the queue is a type of data structure in the Java programming language that stores elements of the same kind. The components in a queue are stored in a FIFO (First In, First Out) behavior. There are two ends in the queue collection, i.e., front & rear. Queue has two ends that is front and rear.

The following figure perfectly describes the FIFO (First In, First Out) property of the Java queue.

Java Queue

As explained in the preceding image, we can see that the queue is a linear data structure with two terminals, i.e., start (front) and end (rear). Components are added inside the queue from the rear end of the queue and the components are extracted from the front end of the queue.

The Queue is an interface in the Java that belongs to Java.util package. It also extends the Collection interface.

The generic representation of the Java Queue interface is shown below:

As we have discussed above that the Queue is an interface, therefore we can also say that the queue cannot be instantiated because interfaces cannot be instantiated. If a user wants to implement the functionality of the Queue interface in Java, then it is mandatory to have some solid classes that implement the Queue interface.

In Java programming language, there are two different classes which are used to implement the Queue interface. These classes are:

Java Queue

Characteristics of the Java Queue

The Java Queue can be considered as one of the most important data structures in the programming world. Java Queue is attractive because of its properties. The significant properties of the Java Queue data structure are given as follows:

  • Java Queue obeys the FIFO (First In, First Out) manner. It indicates that elements are entered in the queue at the end and eliminated from the front.
  • The Java Queue interface gives all the rules and processes of the Collection interface like inclusion, deletion, etc.
  • There are two different classes that are used to implement the Queue interface. These classes are LinkedList and PriorityQueue.
  • Other than these two, there is a class that is, Array Blocking Queue that is used to implement the Queue interface.
  • There are two types of queues, Unbounded queues and Bounded queues. The Queues that are a part of the java.util package are known as the Unbounded queues and bounded queues are the queues that are present in java.util.concurrent package.
  • The Deque or (double-ended queue) is also a type of queue that carries the inclusion and deletion of elements from both ends.
  • The deque is also considered thread-safe.
  • Blocking Queues are also one of the types of queues that are also thread-safe. The Blocking Queues are used to implement the producer-consumer queries.
  • Blocking Queues do not support null elements. In Blocking queues, if any work similar to null values is tried, then the NullPointerException is also thrown.

Implementation of Queue

Classes used in implementation of Queue

The classes that are used to implement the functionalities of the queue are given as follows:

Interfaces used in implementation of Queue

The Java interfaces are also used in the implementation of the Java queue. The interfaces that are used to implement the functionalities of the queue are given as follows:

Java Queue
  • Deque
  • Blocking Queue
  • Blocking Deque
Java Queue

Java Queue Class Methods

In the Java queue, there are many methods that are used very commonly. The Queue interface promotes different methods like insert, delete, peek, etc. Some of the operations of the Java queue raise an exception whereas some of these operations return a particular value when the program is completed.

Note - In Java SE 8, there are no changes made in the Java queue collection. These methods which are defined under are further prepared in the succeeding versions of the Java programming language. For example, Java SE 9.

Different methods of the Java Queue are defined below:

MethodMethod PrototypeDescription
addboolean add(E e)Adds element e to the queue at the end (tail) of the queue without violating the restrictions on the capacity. Returns true if success or IllegalStateException if the capacity is exhausted.
peekE peek()Returns the head (front) of the queue without removing it.
elementE element()Performs the same operation as the peek () method. Throws NoSuchElementException when the queue is empty.
removeE remove()Removes the head of the queue and returns it. Throws NoSuchElementException if queue is empty.
pollE poll()Removes the head of the queue and returns it. If the queue is empty, it returns null.
Offerboolean offer(E e)Insert the new element e into the queue without violating capacity restrictions.
sizeint size()Returns the size or number of elements in the queue.

Java Queue Array Implementation

Queue implementation is not as straightforward as a stack implementation.

To implement queue using Arrays, we first declare an array that holds n number of elements.

Then we define the following operations to be performed in this queue.

1) Enqueue: An operation to insert an element in the queue is Enqueue (function queue Enqueue in the program). For inserting an element at the rear end, we need first to check if the queue is full. If it is full, then we cannot insert the element. If rear < n, then we insert the element in the queue.

2) Dequeue: The operation to delete an element from the queue is Dequeue (function queue Dequeue in the program). First, we check whether the queue is empty. For dequeue operation to work, there has to be at least one element in the queue.

3) Front: This method returns the front of the queue.

4) Display: This method traverses the queue and displays the elements of the queue.

Java Queue Program

The following Java program demonstrates the implementation of Queue.

QueueArrayImplementation.java

Output:

Initial Queue:
Queue is Empty
Queue after Enqueue Operation:
 10 ,  30 ,  50 ,  70 , 
Front Element of the queue: 10
Queue is full
 10 ,  30 ,  50 ,  70 , 
Queue after two dequeue operations: 50 ,  70 , 
Front Element of the queue: 50

Java Queue Linked List Implementation

As we have implemented the Queue data structure using Arrays in the above program, we can also implement the Queue using Linked List.

We will implement the same methods enqueue, dequeue, front, and display in this program. The difference is that we will be using the Linked List data structure instead of Array.

The below program demonstrates the Linked List implementation of Queue in Java.

QueueLLImplementation.java

Output:

Element 6 added to the queue
Element 3 added to the queue
Front of the queue:6 Rear of the queue:3
Element 12 added to the queue
Element 24 added to the queue
Element 6 removed from the queue
Element 3 removed from the queue
Element 9 added to the queue
Front of the queue:12 Rear of the queue:9





Latest Courses