Write a Java Program to Implement the queue data structure

A queue is a data structure that follows the First-In-First-Out (FIFO) principle, which means that the first element added to the queue is the first element to be removed.

Queues are commonly used in computer science to manage jobs, tasks, and requests that need to be processed in order.

In Java, we can implement a queue using the LinkedList class provided in the java.util package.

The LinkedList class provides us with the necessary methods to add and remove elements from both ends of the list, making it an ideal choice for implementing a queue.

To implement a queue in Java, we first need to create a class that represents the queue.

The class will contain a LinkedList object to store the elements of the queue and methods to add and remove elements from the queue.

Here’s an example:

import java.util.LinkedList;

public class Queue<T> {

    private LinkedList<T> queue = new LinkedList<T>();

    public void enqueue(T element) {
        queue.addLast(element);
    }

    public T dequeue() {
        return queue.poll();
    }

    public T peek() {
        return queue.peek();
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }

    public int size() {
        return queue.size();
    }
}

In the above code, we have created a generic class Queue that can be used to store elements of any type T.

The class contains a private LinkedList object called queue to store the elements of the queue.

The enqueue() method adds an element to the end of the queue using the addLast() method provided by the LinkedList class.

The dequeue() method removes and returns the first element of the queue using the poll() method provided by the LinkedList class.

The peek() method returns the first element of the queue without removing it using the peek() method provided by the LinkedList class.

The isEmpty() method checks whether the queue is empty or not using the isEmpty() method provided by the LinkedList class.

The size() method returns the number of elements in the queue using the size() method provided by the LinkedList class.

Now, we can use the Queue class to create an instance of a queue and add or remove elements from it.

Here’s an example:

Queue<String> queue = new Queue<String>();

queue.enqueue("Apple");
queue.enqueue("Banana");
queue.enqueue("Orange");

System.out.println(queue.dequeue()); // Output: Apple
System.out.println(queue.peek());   // Output: Banana
System.out.println(queue.size());   // Output: 2
System.out.println(queue.isEmpty());// Output: false

In the above code, we have created an instance of the Queue class that can store elements of type String.

We have added three fruits to the queue using the enqueue() method, and then we have removed the first element using the dequeue() method.

We have also used the peek() method to see the next element in the queue, the size() method to see how many elements are in the queue, and the isEmpty() method to check if the queue is empty or not.


In conclusion, implementing a queue in Java is straightforward using the LinkedList class provided in the java.util package.

By creating a class that contains a LinkedList object and methods to add and remove elements from the queue, we can easily create a queue that follows the FIFO principle.