LinkedList as a FIFO Queue

suggest change

The java.util.LinkedList class, while implementing java.util.List is a general-purpose implementation of java.util.Queue interface too operating on a FIFO (First In, First Out) principle.

In the example below, with offer() method, the elements are inserted into the LinkedList. This insertion operation is called enqueue. In the while loop below, the elements are removed from the Queue based on FIFO. This operation is called dequeue.

Queue<String> queue = new LinkedList<String>();

queue.offer( "first element" );
queue.offer( "second element" );
queue.offer( "third element" );
queue.offer( "fourth. element" );
queue.offer( "fifth. element" );

while ( !queue.isEmpty() ) {
  System.out.println( queue.poll() );
}

The output of this code is

first element
second element
third element
fourth element
fifth element

As seen in the output, the first inserted element “first element” is removed firstly, “second element” is removed in the second place etc.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents