Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIFO class in Java

Tags:

java

fifo

I want to implement FIFO through a class in Java.

Does such a class already exist? If not, how can I implement my own?

NOTE

I found a class here http://www.dcache.org/manuals/cells/docs/api/dmg/util/Fifo.html, but it doesn't contain dmg.util.*. I don't know if such a package even exists.

like image 292
Rog Matthews Avatar asked Mar 06 '12 08:03

Rog Matthews


People also ask

What is FIFO Java?

FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last.

Is Java set FIFO?

Q #1) What is a Queue in Java? Answer: Queue in Java is a linear ordered data structure that follows FIFO (First In, First Out) ordering of elements. This means that the element inserted first in the queue will be the first element to be removed.

Does Java stack FIFO?

java implements a FIFO queue of strings using a linked list. Like Stack, we maintain a reference first to the least-recently added Node on the queue. For efficiency, we also maintain a reference last to the most-recently added Node on the queue. Resizing array implementation of a queue.

How do you create a queue in FIFO in Java?

For ArrayDeque to function as a queue (FIFO) rather than a stack (LIFO), you should use add and remove . If you use push and pop , it behaves as a stack. (Strictly speaking, remove and pop are the same, but since add/pop or push/remove don't sound good as pairs, we go with add/remove and push/pop .)


1 Answers

You're looking for any class that implements the Queue interface, excluding PriorityQueue and PriorityBlockingQueue, which do not use a FIFO algorithm.

Probably a LinkedList using add (adds one to the end) and removeFirst (removes one from the front and returns it) is the easiest one to use.

For example, here's a program that uses a LinkedList to queue and retrieve the digits of PI:

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();     } }  

Alternatively, if you know you only want to treat it as a queue (without the extra features of a linked list), you can just use the Queue interface itself:

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();     } } 

This has the advantage of allowing you to replace the underlying concrete class with any class that provides the Queue interface, without having to change the code too much.

The basic changes are to change the type of fifo to a Queue and to use remove() instead of removeFirst(), the latter being unavailable for the Queue interface.

Calling isEmpty() is still okay since that belongs to the Collection interface of which Queue is a derivative.

like image 70
paxdiablo Avatar answered Sep 21 '22 05:09

paxdiablo