Javatpoint Logo
Javatpoint Logo

Implementing the Java Queue Interface

The Java Queue interface is a fundamental part of the Java Collections Framework that provides an implementation of a queue data structure. It follows the First-In-First-Out (FIFO) principle, where the elements are inserted at the end and removed from the front. This article will explore the Java Queue interface and demonstrate its implementation using various classes provided by the Java API.

Understanding the Java Queue Interface:

The Queue interface in Java extends the Collection interface, making it a generic class that can hold elements of any type. It defines several methods for enqueueing (adding) and dequeueing (removing) elements from the queue, as well as retrieving the element at the front without removing it.

Implementing the Queue Interface:

To implement the Queue interface in Java, there are several classes available in the Java API, each with its own characteristics and use cases. The most commonly used classes are:

LinkedList:

LinkedList is a doubly-linked list implementation of the Queue interface. It provides efficient insertion and deletion at both ends of the list, making it an ideal choice for implementing a queue. To create a Queue using LinkedList, you can simply instantiate a new LinkedList object and assign it to a Queue reference, as shown below:

ArrayDeque:

ArrayDeque is an array-based implementation of the Deque interface, which itself extends the Queue interface. It provides dynamic resizing and efficient insertion and deletion at both ends of the deque. To create a Queue using ArrayDeque, you can instantiate a new ArrayDeque object and assign it to a Queue reference, as shown below:

PriorityQueue:

PriorityQueue is an implementation of the Queue interface that orders its elements based on their natural order or a specified Comparator. It allows fast retrieval of the minimum (or maximum) element. To create a Queue using PriorityQueue, you can instantiate a new PriorityQueue object and assign it to a Queue reference, as shown below:

Common Operations on Queue:

Once you have created a Queue instance, you can perform several common operations on it:

Enqueuing:

Enqueuing is the process of adding elements to the end of the queue. You can use the add() or offer() method to enqueue elements. Both methods return a boolean value indicating whether the addition was successful.

Dequeuing:

Dequeuing is the process of removing elements from the front of the queue. You can use the remove() or poll() method to dequeue elements. Both methods return the removed element.

Retrieving the Front Element:

To retrieve the element at the front of the queue without removing it, you can use the peek() method. It returns the element, or null if the queue is empty.

Here's a complete Java program that demonstrates the implementation of the Queue interface using the LinkedList class:

File Name: QueueImplementationExample.java

Output:

Initial Queue: [Apple, Banana, Cherry, Durian]
Removed Element: Apple
Queue after Dequeue operation: [Banana, Cherry, Durian]
Front Element: Banana
Queue after Enqueue operation: [Banana, Cherry, Durian, Elderberry]

In this program, we create a Queue using the LinkedList class and enqueue four elements: "Apple", "Banana", "Cherry", and "Durian". Then, we print the initial queue.

Next, we dequeue an element from the front of the queue using the remove() method. The removed element ("Apple") is printed, and the updated queue is displayed.

After that, we use the peek() method to retrieve the front element ("Banana") without removing it. The front element is printed.

Finally, we enqueue another element ("Elderberry") using the add() method, and the updated queue is printed.

Note: The program uses the LinkedList class to implement the Queue interface. You can replace it with other implementations like ArrayDeque or PriorityQueue by modifying the instantiation of the queue object in the program.

Conclusion:

The Java Queue interface is a powerful tool for implementing a queue data structure in Java. By utilizing the available classes provided by the Java API, such as LinkedList, ArrayDeque, and PriorityQueue, you can create efficient and flexible queues tailored to your specific requirements. Whether you need a basic queue or one that orders elements, the Queue interface has you covered. Understanding the implementation options and common operations will help you build robust and efficient applications that make use of this essential data structure.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA