Member-only story
Hi everyone, welcome back. In this tutorial, we will be going over the Priority Queue and how to use the built in functions regarding them. 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. With this introduction out of the way, let’s get into it.
Creating the PriorityQueue
Let’s start by creating a PriorityQueue. Before we can create the PriorityQueue, we need to make sure we have our PriorityQueue import, which is included within the java.util package:
import java.util.PriorityQueue;
Now, we can successfully create our PriorityQueue:
PriorityQueue<Integer> myQ = new PriorityQueue<Integer>();
So now, we have created our PriorityQueue variable of the ‘Integer’ data type and named it ‘myQ.’ When we create our PriorityQueue, we need to specify the data type. As we can see in the example above, we have used the ‘Integer’ data type. We can use other data types as well, such as the ‘String’ data type.