Queue vs Dequeue in java

JavaData StructuresQueueDeque

Java Problem Overview


What is the difference between them? I know that

A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue. Where as Dequeue represents a queue where you can insert and remove elements from both ends of the queue.

But which is more efficient?

Plus what's the difference between them two? Because I have a bit of knowledge about them, what I said above, but I would like to know more about them.

Java Solutions


Solution 1 - Java

Deque is short for "double ended queue". With an ordinary queue, you add things to one end and take them from the other. With a double ended queue, you can add things to either end, and take them from either end. That makes it a bit more versatile; for example, you could use it as a stack if you wanted to.

In terms of efficiency, it really depends on the implementation. But generally speaking, you wouldn't expect a deque to outperform a queue, because a (single ended) queue could be implemented in a way that doesn't allow objects to be added or removed at the "wrong" end. Whereas any implementation of a deque would also work as an implementation of a queue.

Solution 2 - Java

Deque and queue are abstract data types that can be implemented in different ways. To talk about performance you have to specify which implementations you want to compare and which operation(s) you're interested in. Even better, do the benchmark yourself with the workload your application has and in the environment you're going to use (hardware, operating system, JVM version).

Since every deque is also a queue, in general you can say that deques can be at most as good as a queues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6503581View Question on Stackoverflow
Solution 1 - JavaDawood ibn KareemView Answer on Stackoverflow
Solution 2 - JavaJoniView Answer on Stackoverflow