Implementing FIFO Queues in Java with the Queue Interface

Dec 02, 2025 · Programming · 9 views · 7.8

Keywords: Java | FIFO | Queue Interface

Abstract: This article explores the implementation of FIFO (First-In-First-Out) queues in Java, focusing on the Queue interface and its implementation using LinkedList. It compares direct LinkedList usage with programming to the Queue interface, highlighting advantages in maintainability and flexibility. Complete code examples demonstrate enqueuing array elements and sequential dequeuing, along with discussions on methods like isEmpty() from the Collection interface.

In Java programming, implementing FIFO (First-In-First-Out) queues is a common requirement, especially in scenarios like task scheduling and data buffering. The Java Standard Library provides various mechanisms to support queue operations, with the Queue interface being a key abstraction. Based on best practices, this article details how to leverage existing classes for FIFO implementation and examines the impact of design choices on code quality.

Queue Interface and FIFO Implementation

Java's java.util.Queue interface defines basic queue operations such as adding, removing, and inspecting elements. It extends the Collection interface, ensuring consistency. For FIFO queues, avoid using PriorityQueue or PriorityBlockingQueue, as they are based on priority rather than first-in-first-out order. Common FIFO implementations include LinkedList and ArrayDeque, with LinkedList widely adopted for its dynamic nature and ease of use.

Direct Implementation Using LinkedList

FIFO queues can be directly implemented using the LinkedList class, utilizing its add method to append elements at the end and removeFirst to remove elements from the front. The following example demonstrates enqueuing a character array and dequeuing sequentially to simulate data stream processing:

import java.util.LinkedList;

class Test {
    public static void main(String args[]) {
        char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9};
        LinkedList<Integer> fifo = new LinkedList<Integer>();

        for (int i = 0; i < arr.length; i++)
            fifo.add(new Integer(arr[i]));

        System.out.print(fifo.removeFirst() + ".");
        while (!fifo.isEmpty())
            System.out.print(fifo.removeFirst());
        System.out.println();
    }
}

This approach is straightforward but ties the code to the specific implementation of LinkedList, reducing flexibility. For instance, if switching to ArrayDeque in the future, multiple code changes would be required.

Abstract Implementation via the Queue Interface

To enhance code maintainability and extensibility, it is recommended to program to the Queue interface. This abstracts the concrete implementation, facilitating future replacements. The example below shows how to achieve the same functionality using the Queue interface:

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

class Test {
    public static void main(String args[]) {
        char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9};
        Queue<Integer> fifo = new LinkedList<Integer>();

        for (int i = 0; i < arr.length; i++)
            fifo.add(new Integer(arr[i]));

        System.out.print(fifo.remove() + ".");
        while (!fifo.isEmpty())
            System.out.print(fifo.remove());
        System.out.println();
    }
}

Here, the fifo variable is declared as type Queue<Integer> and initialized with LinkedList. Operations use add and remove methods, where remove in the Queue interface is equivalent to removeFirst. Note that isEmpty() is available as it is inherited from the Collection interface. This approach allows easy switching to, for example, ArrayDeque by only modifying the initialization, leaving the rest of the code intact.

Core Knowledge and Best Practices

When implementing FIFO queues, prioritize the Queue interface to adhere to interface-oriented programming principles. Key considerations include selecting non-priority queue implementations (e.g., LinkedList or ArrayDeque), using generics for type safety, and employing isEmpty() for empty queue checks. In practical applications, thread safety should also be considered, such as using ConcurrentLinkedQueue in concurrent environments. Abstracting through interfaces makes code more testable and maintainable, aligning with software engineering best practices.

In summary, FIFO queue implementation in Java can be based directly on LinkedList or abstracted via the Queue interface. The latter offers greater flexibility and code quality, making it recommended for project development. By choosing appropriate implementations based on specific needs like performance or concurrency, program efficiency and reliability can be significantly enhanced.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.