Queue

A queue is a collection in which elements are stored and retrieved in a specific order.

First-in, first-out (FIFO) queue: Elements are inserted at the queue's tail and removed at the queue's head.

This interface is a member of the Java Collections Framework.

boolean add(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
E element()
Retrieves, but does not remove, the head of this queue.
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
E remove()
Retrieves and removes the head of this queue.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {

  public static void main(String[] args) {
    Queue<String> queue = new LinkedList<String>();
    queue.add("A");
    queue.add("B");
    queue.add("ja v a2s.com"); 


    System.out.println(queue);
  }

}
  

The output:


[A, B, ja v a2s.com]
Home 
  Java Book 
    Collection