FIFO ( First | Java program to demonstrate working of FIFO using Queue interface in Java ; Driver code ; Adds elements { 0 , 1 , 2 , 3 , 4 } to queue ; Display contents of the queue . ; To remove the head of queue . In this the oldest element '0' will be removed ; To view the head of queue ; Rest all methods of collection interface , Like size and contains can be used with this implementation .
import java . util . LinkedList ; import java . util . Queue ; public class QueueExample { public static void main ( String [ ] args ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) q . add ( i ) ; System . out . println ( " Elements ▁ of ▁ queue - " + q ) ; int removedele = q . remove ( ) ; System . out . println ( " removed ▁ element - " + removedele ) ; System . out . println ( q ) ; int head = q . peek ( ) ; System . out . println ( " head ▁ of ▁ queue - " + head ) ; int size = q . size ( ) ; System . out . println ( " Size ▁ of ▁ queue - " + size ) ; } }