One of the common questions I hear from Java newcomers is - where is a FIFO list in Java?

Java does have a FIFO list capability built-in with LinkedList and ArrayList, but they are not well advertized.

A FIFO interface should at least have:


public interface FIFO {
    /** Add an object to the end of the FIFO queue */
    boolean add(Object o);

    /** Remove an object from the front of the FIFO queue */
    Object remove();

    /** Return the number of elements in the FIFO queue */
    int size();
}

A FIFOList class implementing the above would simply be:


public class FIFOList extends LinkedList implements FIFO {
    public Object remove() {
        return remove(0);
    }
}

I prefer this setup instead of using a LinkedList.remove(0) directly. It looks cleaner.

BTW: You can also extend an ArrayList instead of LinkedList to achieve the same functionality. LinkedList should in theory provide better performance.