Instantiate Queue in Java

Bishal Awasthi Jun 15, 2021
  1. Use the LinkedList Class to Implement a Queue Object in Java
  2. Use the ArrayDeque Class to Implement a Queue Object in Java
Instantiate Queue in Java

This article will introduce methods to instantiate a Queue object in Java. We will further describe and implement the method calls from the instantiated object in this tutorial. A Queue is an interface, and we cannot create an object directly. But we can implement the class that already implements the Queue interface. Some of those classes are AbstractQueue, ArrayDeque, DelayQueue, LinkedList. Therefore, we will be using the LinkedList and ArrayDeque classes to instantiate Queue in this tutorial.

Use the LinkedList Class to Implement a Queue Object in Java

The LinkedList class implements the LinkedList data structure concept. It is a part of the collection framework and available in the java.util package. The elements in the LinkedList are stored in the form of pointers and addresses. The element is known as a node. We cannot access the node directly in LinkedList. To access a node, we have to start from the head and follow through to reach a node that we want to access. The LinkedList implements the List interface, and it inherits all the method presents in the List interface and collection interface. Queue is also available in the same package. We can create an object of Queue implementing the LinkedList class. We can use the add() function to append the data in the queue.

Firstly, import Queue and LinkedList using the import java.util.Queue and the import java.util.LinkedList respectively. Then, create a class QueueDemo and declare the main method. Next, instantiate an object queue by implementing the LinkedList class. Call the add() method with the object and add five integer values. Finally, print the elements of the Queue.

In the example, we used a LinkedList class that implements the Queue interface to create a queue object. This is because the Queue is an interface, and we cannot create its object directly. We have specified the generic type Integer while creating the object because we store the integer value in the queue. The output section displays the elements stored in the queue.

Example Code:

import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(8); 
        queue.add(9);
        queue.add(10);
        queue.add(2);
        queue.add(5);
        System.out.println("Added Queue in memory: " +queue); 
    }
}

Output:

Added Queue in memory: [8, 9, 10, 2, 5]

Use the ArrayDeque Class to Implement a Queue Object in Java

Java language has defined the ArrayDeque class as a part of the collection framework. It creates an empty set of locations in memory with an initial capacity sufficient to hold sixteen elements. ArrayDeque is also called Array Double Ended Queue, which allows us to insert or delete an element from both sides. It implements a Stack (Last-In-First-Out) or a Queue(First-In-First-Out). Therefore, we can create an object of Queue with the implementation of the ArrayDeque class.

Import the packages as above but make sure you import the ArrayDeque in this method instead of the LinkedList. Then, create a class Demo and declare the main method. Then implement the ArrayDeque class by instantiating the Queue interface. Create an object of Queue as aq. Use the aq object to call the add() method four times. Append four different strings. Finally, print the aq object.

In the example below, we have used the generic type String while creating the object. It is because we are adding string items in the queue. Thus, we have instantiated a Queue object implementing the ArrayDeque class.

Example code:

import java.util.ArrayDeque;
import java.util.Queue;
public class Demo {
    public static void main(String[] args) {
      Queue<String> aq = new ArrayDeque<String>(); 
      aq.add("first"); 
      aq.add("second");
      aq.add("third");
      aq.add("fourth");
      System.out.println("Added Queue in memory: " +aq);     
    }
}

Output:

Added Queue in memory: [first, second, third, fourth]

Related Article - Java Queue