How to Implement a FIFO (First-In-First-Out) Queue in Java? Last Updated : 14 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report FIFO stands for First In, First Out, which is a common way of organizing and manipulating Data Structure. In FIFO, the first element that is added to the data structure is the same first element that is to be removed first. Queue is a FIFO-based Data Structure. Here, the insert operation is called Enque, and it is added always at the start, and the delete operation is called the Deque and it is the first element that is removed. Implementation of FIFO-based QueueIn this article, we will be using a dynamic array to implement our queue. Here, the index is pointing towards the head of the queue. As mentioned, that queue should support both enque and deque, so we need a starting index to show the start point. Program to Implement a FIFO-based Queue in JavaBelow is the implemented code in Java: Java // Java Program to implement a FIFO-based Queue import java.util.*; import java.util.Queue; class MyQueue { // Store Elements private List<Integer> data; private int p_start; public MyQueue() { data = new ArrayList<Integer>(); p_start = 0; } public boolean enQueue(int x) { data.add(x); return true; }; // Delete the item from queue public boolean deQueue() { if (isEmpty() == true) { return false; } p_start++; return true; } // Find the first item from the queue public int Front() { return data.get(p_start); } public boolean isEmpty() { return p_start >= data.size(); } }; // Driver Class public class Main { // Main Function public static void main(String[] args) { MyQueue q = new MyQueue(); q.enQueue(5); q.enQueue(3); if (q.isEmpty() == false) { System.out.println(q.Front()); } q.deQueue(); if (q.isEmpty() == false) { System.out.println(q.Front()); } q.deQueue(); if (q.isEmpty() == false) { System.out.println(q.Front()); } } } Output5 3 Explanation of the Program:The above Java program implements a basic queue data structure using a list. The MyQueue class includes methods to enque, deque, and checks if the queue is empty, and retrieve the front element of the queue. It uses a list (data) to store the elements and a pointer (p_start) to keep track of the front element.In the Main class, an instance of MyQueue is created, elements are enqueued (enQueue), and then dequeued (deQueue). The front element of the queue is printed after each operation if the queue is not empty.The third attempt to output the front element will not produce any output as the queue becomes empty after the second deque operation.Complexities of the Program mentioned above:The overall time complexity for the enque, deque here is O(1). It is because the operation involves the constant operation time involvement, like accessing the elements directly.The overall space complexity is O(n), because there is n number of elements stored inside the queue. Comment More infoAdvertise with us Next Article How to Iterate over a Queue in Java? M milindgupta578 Follow Improve Article Tags : Java Java Programs java-queue Java Examples Practice Tags : Java Similar Reads How to Implement a Circular Queue in Java ? A Circular Queue is a queue in which we can insert an element at the start of the Array even if our rare is reached at the last index and if we have space at the start of the array. This reduces the problem of inefficient use of array space. Once the array is filled till the last and if we have spac 4 min read How to Implement Queue in Java using Array and Generics? The queue is a linear data structure that follows the FIFO rule (first in first out). We can implement Queue for not only Integers but also Strings, Float, or Characters. There are 5 primary operations in Queue: enqueue() adds element x to the front of the queuedequeue() removes the last element of 4 min read Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read How to Iterate over a Queue in Java? Queue is a concept of Linear Data Structure that follows the concept of FIFO(First In First Out). Queues are majorly used to maintain the order of the elements to which they are added. In this article, we will learn to perform Queue Iteration in Java. Queue Iteration Program in JavaSyntax with Examp 1 min read Conversion of a List to a Queue in Java In this article, We will learn about how to convert a List to a Queue in Java using various methods. PrerequisiteQueue Interface in JavaList Interface in JavaMethods to Convert List to Queue in JavaIn Java, there are a few ways to convert a List to a Queue. Here are some methods and ways you can ach 3 min read How to Configure Java Priority Queue to Handle Duplicate Elements? In Java, PriorityQueue is the data structure that stores the elements based on their priorities and provides operations to efficiently retrieve and manipulate these elements based on their priorities. PriorityQueue handles the duplicate elements using a comparator. Comparator is a pre-defined interf 3 min read Like