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.
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 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:
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:
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 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:
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.
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.
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