FIFO class in Java
Asked Answered
K

7

76

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.

Karakorum answered 6/3, 2012 at 8:50 Comment(0)
E
137

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.

Extravert answered 6/3, 2012 at 8:52 Comment(5)
Why not make fifo be of type Queue<Integer> then? Target the interface rather than the concrete implementation.Panto
If you just want to iterate over the items in the queue in a FIFO manner without actually removing the items, then you can just do for (Object item : queue), and it will iterate over them in a FIFO manner, at least on JDK 7 and with ArrayDeQueue and LinkedList impl.Fixate
Re "You're looking for any class that implements the Queue interface". This is not correct. If someone is looking for FIFO, then they DON'T want PriorityQueue or PriorityBlockingQueue, because those don't do FIFO; they have a different ordering algorithm. On a lesser note, if someone is thinking I want a simple FIFO queue, then they likely don't want SynchronousQueue. Arguably, they don't even want any of the Blocking queues, though this is a more debatable claim. This reduces it down to two choices: LinkedList and ArrayDeQueue!Mcreynolds
@ClickUpvote - pondering whether that syntax is a good thing or a bad thing. My first thought in seeing that line, is that I would think it was handling those items, e.g. removing them from the queue. IMHO anyone who uses that syntax should put a COMMENT above that code line, saying that this is peeking at the items, not removing them. Nevertheless, it is a handy trick to know.Mcreynolds
@Mcreynolds you must be new to java or java 7 if you think that syntax is removing things. that syntax is very common in java for looping over pretty much anything without removing them. its also recommended over for / while loops that just loop over somethingFixate
I
19

Try ArrayDeque or LinkedList, which both implement the Queue interface.

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayDeque.html

Irvine answered 6/3, 2012 at 8:53 Comment(2)
Yes, ArrayDeque and LinkedList have the .push method from Deque, which is what makes it usable as a Stack for FIFO. ArrayDeque is more like a primitive array to use as a buffer whereas LinkedList is more like a data-store.Grays
@Grays stack is lifoDiscontented
C
2

Queues are First In First Out structures. You request is pretty vague, but I am guessing that you need only the basic functionality which usually comes out with Queue structures. You can take a look at how you can implement it here.

With regards to your missing package, it is most likely because you will need to either download or create the package yourself by following that tutorial.

Concerted answered 6/3, 2012 at 8:53 Comment(0)
T
1

You don't have to implement your own FIFO Queue, just look at the interface java.util.Queue and its implementations

Treece answered 6/3, 2012 at 8:54 Comment(0)
K
1

if you want to have a pipe to write/read data, you can use the http://docs.oracle.com/javase/6/docs/api/java/io/PipedWriter.html

Kerstin answered 6/3, 2012 at 8:55 Comment(0)
J
0

You can use LinkedBlockingQueue I use it in my projects. It's part of standard java and quite easy to use

Jit answered 25/11, 2014 at 21:5 Comment(0)
P
-1

Not sure what you call FIFO these days since Queue is FILO, but when I was a student we used the Stack<E> with the simple push, pop, and a peek... It is really that simple, no need for complicating further with Queue and whatever the accepted answer suggests.

Purehearted answered 18/12, 2020 at 2:21 Comment(2)
Stack is defined as a last-in-first-out implementation while Queue implementations are first-in-first-out by default unless otherwise indicated with a comparator.Vivica
If first in and last out always (un)occupies the same slot, then I didn't remember it properly. Otherwise, java.util.Stack simply doesn't work that way. See: docs.oracle.com/javase/7/docs/api/java/util/Stack.htmlPurehearted

© 2022 - 2025 — McMap. All rights reserved.