FIFO Queue in Java

A queue is a linear data structure that implements the first-in first-out concept. The concept means that the elements that get in first are the first to come out.

The technical terms to represent the Queue are using the front and rear ends. The elements are added to the Queue from the rear end and get taken out from the front.

The real-world example of the concept is the ticket counter row, where the person who stands at the first position gets the ticket first. Now in Java, the concept or the data structure gets implemented in the Queue interface. The interface is present in the java.util package.

Below is the code block that demonstrates the queue structure.

package F10;

import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue queue = new LinkedList<String>();
        System.out.println("Created Queue: " + queue);
        String value = (String) queue.remove();
        System.out.println("The element deleted from the head is: " + value);
        System.out.println("The queue after deletion: " + queue);
        String head = (String) queue.peek();
        System.out.println("The head of the present queue is: " + head);
        int size = queue.size();
        System.out.println("The size of the queue is: " + size);

There are various ways to implement the Queue data structure in Java. In the code block above, the use of LinkedList implements the concept.

First, a Queue reference gets instantiated using the new LinkedList statement. The add method is present in the Queue interface, which inserts the specified element into the Queue. The function returns Boolean true when the value gets appended.

The method can throw the IllegalStateException when the element cannot be added due to size restrictions. It also throws NullPointerException when the value passed is null. When the initial set of elements gets appended into the list, then the Queue gets printed. The System.out.println statement takes the string that needs to get printed.

Next, the remove function is called over the Queue instance. It will remove the element present on the head of the Queue and hence, returns the head element.

There is a similar poll function that also removes the element present on the head. The only difference lies in the remove function that throws NoSuchElementException when the Queue is empty. The removed values are stored in a variable and printed. The remaining Queue is printed to check the remaining elements.

The peek function retrieves the topmost element in the Queue and does not remove it; it is a way to check the element on the head of the Queue. The function returns the head value and throws NoSuchElementException when the Queue is empty. The size function is present in the Collection interface and returns the size of the collection. So, this method prints the size of leftover elements in the Queue.


Created Queue: [First, Second, third]
The element deleted from the head is: First
The Queue after deletion: [Second, third]
The head of the present Queue is: Second
The size of the Queue is: 2
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Queue

  • PriorityQueue in Java