How to Code FIFO in Java
- 1). Open "Netbeans." Click "File" and "New class."
- 2). Type "psvm" to create a main method.
- 3). Add the following code to the main method:
LinkedList<String> fifo = new LinkedList<String>();
fifo.offer("This is first.");
fifo.offer("This is second.");
fifo.offer("This is third.");
System.out.println(fifo.poll());
System.out.println(fifo.poll());
System.out.println(fifo.poll());
The "offer" method adds the new data to the end of the queue. The "add" method can also be used, and can optionally specify a position in the list for the new element. The "poll" method will both return the item at the head of the queue and remove it from the list entirely. If you want to look at the item at the front of the list, but not have it removed, you would use the "peak" method instead.
Source...