Implementation of the queue can be based on FIFO , priorities and LIFO - official documentation says this.
When the programmer first sees the " Queue ", he automatically considers that " it must be a FIFO order " ( or, ultimately, a priority order ). But, as the documentation says, it should be possible to use the Queue interface to streamline the LIFO. Let me explain to you how this can be done.
// FIFO queue usage Queue<Integer> queue = new LinkedList<>(); queue.add(1); queue.add(2); queue.remove(); // returns 1 queue.remove(); // returns 2 // LIFO queue usage Queue<Integer> queue = Collections.asLifoQueue(new ArrayDeque<>()); queue.add(1); queue.add(2); queue.remove(); // returns 2 queue.remove(); // returns 1
As you can see, depending on the implementation, the Queue interface can also be used as a LIFO.
source share