Code: FIFO List in Java
By Angsuman Chakraborty, Gaea News NetworkSaturday, February 25, 2006
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.
April 27, 2008: 1:21 am
As I said before: |
April 26, 2008: 1:41 pm
You could have just used LinkedList. Method add adds element to the end. And to retrieve element use getFirst |
![]() cherlyn |
February 17, 2008: 6:14 pm
can you send me a copy of your codes in FIFO which deals with the page(s). please grant me my request..thank you.!!! |
February 27, 2006: 9:07 am
You are right. I overlooked them in 1.5. In fact ArrayList also implements Queue. |
![]() afsina |
February 26, 2006: 7:54 pm
well.. isnt fifo is actually a Queue? there are plenty of Queues available in Java5. |
February 26, 2006: 11:04 am
A nice solution for a FIFO queue is a wrapper around a circular array. Removes at either end are cheap, inserts at either end are also cheap. Memory usage is low and Object allocation is infrequent. |
vivoconunxino