Java Programming Language: Queue Tutorial
Hi everyone, welcome back. In this tutorial, I will show how to use the built in functions regarding the queue in Java. A queue is a linear type of data structure that is capable of storing objects. The Queue is a first-in-first-out or FIFO data structure which means that the first item added to the queue is the first item that can be removed. Let’s say the numbers 1, 2 , and 3 are added into a queue in that order. If the items start to get removed, the first item to leave would be 1, then 2, then finally 3.

Queue
The Queue is already an interface that is provided by Java, but we will have to use a PriorityQueue, which is a class that implements the Queue in our project. We can import it into our project:
import java.util.PriorityQueue;
The PriorityQueue class allows us to create an empty queue. Creating a queue is similar to creating other objects in Java.
PriorityQueue q = new PriorityQueue();
Now let’s go over some of the built in functions that Java provides.
isEmpty()
The first function we will go over is the isEmpty() function which checks to see whether or not the queue is empty. The isEmpty() function returns a boolean.
PriorityQueue q = new PriorityQueue();
System.out.println(q.isEmpty());Output:
true
As you can see above, we created a queue with no values added to it and checked to see if it was empty. Our print statement shows that the q.isEmpty() function is returning true, which is our expected response.
add(object)
Now let’s add an object into the queue. We can do this by using the add() function. In this example, I will add 1, 2, and 3 into the queue:
PriorityQueue q = new PriorityQueue();
q.add(1);
q.add(2);
q.add(3);
System.out.println(q);
System.out.println(q.isEmpty());Output:
[1, 2, 3]
false
Our output shows that we have successfully added values of 1, 2, and 3 into the queue. Our isEmpty() function is also returning a false value now.
peek()
The peek() function allows us to view the first item in the queue. Let’s see an example:
PriorityQueue q = new PriorityQueue();
q.add(1);
q.add(2);
q.add(3);
System.out.println(q.peek());Output:
1
We have added 1, 2, and 3 into our queue in that order. The peek() function has returned the first item in our queue, in this case it was 1.
remove()
The remove() function is used to remove an item from the queue. But the queue only removes from the front, or the first item that was added. Let’s see how it works:
PriorityQueue q = new PriorityQueue();
q.add(1);
q.add(2);
q.add(3);
System.out.println(q);Output:
[1, 2, 3]q.remove();
System.out.println(q);Output:
[2, 3]
We have again added 1, 2, and 3 into our queue and our first print statement is displaying all three numbers. But then we call the remove() function which removes an item from the queue. The queue removes the first item that was added, in our case the 1 was the first item added. Our second print statement verifies that the 1 was indeed removed from the queue.
This is it for the tutorial. We covered the queue data structure and some basic built-in functions that can be used with the queue. I hope this helps. Thanks for reading.