Enqueue and Dequeue in Java

Before we get into the concepts of enqueue
and dequeue
in Java, let’s build up a basic understanding of the whole concept of Queue. What is it actually? The real-world example and much more. Let’s dive in.
Queue
Queue in terms of programming is a linear structure with a one-way order through which this datatype performs. The one-way order is FIFO (first in, first out). A real-world example would be the line of people (queue) you see outside a bank. Whoever comes in first gets served first and goes out. So, when we say enqueue
and dequeue
in programming, we mean adding and removing an item, respectively. Take a look at the picture down below.
As you can see, addition (enqueue) in the queue will always be from the back, and removal (dequeue) will always be from the front. Now that you have a concrete understanding of the queue, let’s take a look at the implementation of the queue in Java.
Queue in Java
In Java, the queue is regarded as an interface that is implemented by a linked list. The following code example shows how one can create the object of the queue in Java.
import java.util.LinkedList; // Class Which implements queue list etc.
import java.util.Queue; //queue is an iterface
public class Main
{
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<>();
//to implement Queue
}
}
You need to add the classes of linked lists and queues to implement one in Java. (See at the top of the code). While implementing queue, we use Linked List for this purpose. Now let’s understand the methods involved in adding and removing an item from the queue.
Enqueue and Dequeue in Java
As mentioned above, the order for these types of operations is FIFO. So, Let’s add (enqueue
) some value in the queue we made. As the queue is a string, the following values would suffice.
import java.util.LinkedList; // Class Which implements queue list etc.
import java.util.Queue; //queue is an iterface
public class Main
{
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<>();
//to implement Queue
//Adding In Queue using `add()` method
queue.add("Bill Gates"); //enque
queue.add("Mark Mark Zuckerberg"); // enque
queue.add("Elon Musk"); //enquue
queue.add("Jeff Bezos");
queue.offer("Donald Trump"); // alternatice enque
System.out.println("Queue : " + queue); //queue print
}
}
In the above example, we have added four values using the following two methods.
queue.add()
As you can see in the code, we have added four string values in the queue. Another thing that you should know about this method is that it throws an unchecked exception. For instance, if your queue is restricted, the add()
method will return an exception upon adding another element while there’s no space for it. It all depends on the nature of the queue. Learn more about queues in Java here.
queue.offer()
The offer()
method is an alternative of add()
. This particular method does not throw exceptions. Instead, it gives true and false values. We added the fifth element in the queue using offer()
in the above example. The output of the above program will be as follows.
Queue : [Bill Gates, Mark Zuckerberg, Elon Musk, Jeff Bezos, Donald Trump]
Now that you have understood how you can (enqueue
) add elements in the queue. Let’s see how you can remove an item from the queue.
queue.remove()
The remove()
method is used to delete an element from the queue. It will be operated at the very front of the queue. For instance, if we were to remove an element from the above queue, the first element to get removed would be Bill Gates
. Take a look at the following code.
import java.util.LinkedList; // Class Which implements queue list etc.
import java.util.Queue; //queue is an iterface
public class Main
{
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<>();
//to implement Queue
//Adding In Queue using `add()` method
queue.add("Bill Gates"); //enque
queue.add("Mark Mark Zuckerberg"); // enque
queue.add("Elon Musk"); //enquue
queue.add("Jeff Bezos");
queue.offer("Donald Trump"); // alternatice enque
System.out.println("Queue : " + queue); //queue print
String name = queue.remove(); //Dequeue
System.out.println("Removed from queue : " + name );
System.out.println(queue);
name= queue.poll(); // altrenative deque method
System.out.println("Removed from queue : " + name );
System.out.println(queue);
}
}
As you can see in the above code example, we used the remove()
method in order to delete an item from the queue. It will throw an unchecked exception if you try to delete an element from an empty queue.
queue.poll()
The alternative method of remove()
is poll()
which returns NULL
value upon deleting an element from an empty queue. Take a look at the output of the above code.
Queue : [Bill Gates, Mark Mark Zuckerberg, Elon Musk, Jeff Bezos, Donald Trump]
Removed from queue : Bill Gates
[Mark Mark Zuckerberg, Elon Musk, Jeff Bezos, Donald Trump]
Removed from queue : Mark Mark Zuckerberg
[Elon Musk, Jeff Bezos, Donald Trump]
Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.
LinkedInRelated Article - Java Queue
- Concurrent Queue Implementation in Java
- Java Max Priority Queue
- PriorityQueue in Java
- Queue offer vs add in Java
- FIFO Queue in Java