How implementation of java.util.queue uses LIFO?

celsowm picture celsowm · Jul 26, 2011 · Viewed 19.1k times · Source

In Java doc:

[...] Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out)

How implementation of java.util.queue uses LIFO instead of FIFO?

Answer

ZhekaKozlov picture ZhekaKozlov · Jul 30, 2013

You can use any Deque as a LIFO queue using Collections.asLifoQueue method:

Queue<Integer> arrayLifoQueue = Collections.asLifoQueue(new ArrayDeque<Integer>());
Queue<Integer> linkedListLifoQueue = Collections.asLifoQueue(new LinkedList<Integer>());