Java - Create a stack from a Deque

Introduction

You can convert a Deque to a LIFO Queue using the asLifoQueue() static method of the Collections class.

The method signature is as follows:

public static <T> Queue<T> asLifoQueue(Deque<T> deque)

The following code creates a stack from a Deque:

Deque<String> deque = create a Deque ;
// Get a LIFO queue from Deque
Queue<String> stack = Collections.asLifoQueue(deque);

You can pass around stack reference, which can be used only as a LIFO queue.

Related Topic