Using Java Queue via LinkedList

Getting Queue Element From Head For Inspecting

1. About the Linked Queue

The Queue operates on ‘First In First Out (FIFO)’ principle. This means an element added first to the Queue removed first from it. For example, in a railway ticket counter the person who enters the queue first will be served first and removed from it. Each new person coming to buy the ticket will stand in the tail end of the queue. The same way Queue will work in Java. In simple words, elements are added to it in the tail end and removed from the head .

In this example, we will see how to use Java Linked as a Queue.

2. Queue Methods

Let us see some frequently used methods of the Java Queue. We know the Java LinkedList implements the list interface. It also implements Dequeue interface, and this interface is extending from the Queue interface. In this example, we will explore Queue and in the next article we will learn about Dequeue interface. Now have a look at the Initial LinkedList below:

Initial LinkedList Queue
Initial LinkedList Queue

2.1 Queue.add Method

The add method of the Queue adds an element towards the tails end. Note, we are using the LinkedList as a queue. The Queue adds the new element, in our case, Obj4 in its tails end. That is; it adds the element as a last element. Then it arranged the links for this element. The below picture shows the added element:

Adding Element to LinkedList Queue
Adding Element to LinkedList Queue

2.2 Queue.remove Method

The remove method removes an item from the front of the LinkedList Queue. Means, it removes the head item, leaving the second element as new head item. Below is the LinkedList after calling the remove method:

Removing Element From LinkedList Queue
Removing Element From LinkedList Queue

2.3 Using element method in LinkedList Queue

The element method retrieves the head element from the linked list Queue. But it will not remove it from the Linked-list Queue. We can use this method to inspect the element in the head.

Getting Queue Element From Head For Inspecting
Getting Queue Element From Head For Inspecting

3. LinkedList Queue – Example

3.1 The Product Class

We use the same Product class, which we used in the previous examples for ArrayList and LinkedList as List. You can refer the code for the product class here: ArrayList.

3.2 Create Products & add to the Java’s LinkedList Queue

We create six Product objects and add it to the queue. The queue places each new element towards the tail. In the below java code, we add the product 106 as a last item and this will stay towards the end. When we print the LinkedList, it will print the items in the order in which we added them to the queue.

3.3 Removing Three Products From LinkedList Queue

Next, we make call the Queue’s remove method three times, and this will remove the elements from head of the queue. First it removes Pen, then Pencil and third time it removes Rubber. After the consecutive call to remove three times, the item in the front is Writing Pad. The below code snippet shows how items to removed from the head to adhere to FIFO principal:

4. Code Reference

Below is the complete example for the LinkedList used as Queue:

5. Watch Java Queue example in YouTube

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.