73

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.

canolucas
  • 1,482
  • 1
  • 15
  • 32
Rog Matthews
  • 3,147
  • 16
  • 37
  • 56

7 Answers7

135

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.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    Why not make fifo be of type Queue then? Target the interface rather than the concrete implementation. – Adam Parkin Mar 15 '13 at 21:15
  • 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. – Ali Oct 13 '13 at 10:52
  • 3
    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`! – ToolmakerSteve Sep 11 '14 at 17:53
  • @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. – ToolmakerSteve Sep 11 '14 at 17:54
  • @ToolmakerSteve 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 something – Ali Sep 12 '14 at 13:56
19

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

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

Rusty
  • 454
  • 3
  • 7
  • 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. – djangofan Jun 17 '17 at 18:17
  • 1
    @djangofan stack is lifo – Austin_Anderson Oct 03 '17 at 21:10
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.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

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

ftr
  • 2,105
  • 16
  • 29
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

Hachi
  • 3,237
  • 1
  • 21
  • 29
0

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

kalgecin
  • 93
  • 1
  • 9
-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.

3xCh1_23
  • 1,491
  • 1
  • 20
  • 39
  • 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. – Clement Osei Tano Jan 19 '21 at 08:54
  • 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: https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html – 3xCh1_23 Jan 20 '21 at 13:45