Use:
Queue<Object> queue = new LinkedList<>();
You can use .offer(E e)
to append an element to the end of the queue and .poll()
to dequeue and retrieve the head (first element) of the queue.
Java defined the interface Queue
, the LinkedList provided an implementation.
It also maintains references to the Head and Tail elements, which you can get by .getFirst()
and .getLast()
respectively.
credit to @Snicolas for suggesting queue interface