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:

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:

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:

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.

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample01: Create LinkedList & use it as Queue Queue<Product> Products = new LinkedList<Product>(); //Sample 02: Construct Sample Products Product p1 = new Product(101, "Pen"); Product p2 = new Product(102, "Pencil"); Product p3 = new Product(103, "Rubber"); Product p4 = new Product(104, "Writing Pad"); Product p5 = new Product(105, "Clips"); Product p6 = new Product(106, "Sharpner"); //Sample 03: Add Product to ArrayList Products.add(p1); Products.add(p2); Products.add(p3); Products.add(p4); Products.add(p5); Products.add(p6); printProducts(Products); |
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:
1 2 3 4 |
//Sample 04: Remove Product from the Queue. Removes Head Products.remove(); Products.remove(); Products.remove(); |
4. Code Reference
Below is the complete example for the LinkedList used as Queue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.LinkedList; import java.util.List; import java.util.Queue; public class LinkedListTest { public static void main(String[] args) { //Sample01: Create LinkedList & use it as Queue Queue<Product> Products = new LinkedList<Product>(); //Sample 02: Construct Sample Products Product p1 = new Product(101, "Pen"); Product p2 = new Product(102, "Pencil"); Product p3 = new Product(103, "Rubber"); Product p4 = new Product(104, "Writing Pad"); Product p5 = new Product(105, "Clips"); Product p6 = new Product(106, "Sharpner"); //Sample 03: Add Product to ArrayList Products.add(p1); Products.add(p2); Products.add(p3); Products.add(p4); Products.add(p5); Products.add(p6); printProducts(Products); //Sample 04: Remove Product from the Queue. Removes Head Products.remove(); Products.remove(); Products.remove(); //Sample 05: Add Again, These products go to the tail end Products.add(p1); Products.add(p2); Products.add(p3); //Sample 06: Take the Head element to Test. Head not removed. Product prod1 = Products.element(); Product prod2 = Products.element(); System.out.println("The Head Node is: " + prod1); System.out.println("The Head Node is: " + prod1); } //Sample 04: List the Products private static void printProducts(Queue<Product> Products) { System.out.println("Product Details:-"); for(Product prod: Products){ System.out.println(prod); } System.out.println("=================="); } } |
5. Watch Java Queue example in YouTube
Categories: Java
Tags: add, LinkedList, Queue, remove