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.
You’re looking for any class that implements the Queue interface, excluding
PriorityQueueandPriorityBlockingQueue, which do not use a FIFO algorithm.Probably a LinkedList using
add(adds one to the end) andremoveFirst(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:
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
Queueinterface itself:This has the advantage of allowing you to replace the underlying concrete class with any class that provides the
Queueinterface, without having to change the code too much.The basic changes are to change the type of
fifoto aQueueand to useremove()instead ofremoveFirst(), the latter being unavailable for theQueueinterface.Calling
isEmpty()is still okay since that belongs to theCollectioninterface of whichQueueis a derivative.