java中的队列与出队

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38812225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:40:19  来源:igfitidea点击:

Queue vs Dequeue in java

javadata-structuresqueuedeque

提问by

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.

队列被设计为在队列的末尾插入元素,并从队列的开头删除元素。Dequeue 代表一个队列,您可以在其中插入和删除队列两端的元素。

But which is more efficient?

但哪个更有效率?

Plus what's the difference between them two? cause i have a bit of knowledge about them, what i said above, but i would like to know more about them. It will be appreciated.

另外他们两个有什么区别?因为我对他们有一点了解,我上面说过,但我想更多地了解他们。将不胜感激。

回答by Joni

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).

Deque 和 queue 是可以以不同方式实现的抽象数据类型。要谈论性能,您必须指定要比较的实现以及您感兴趣的操作。更好的是,根据您的应用程序的工作负载和您将要使用的环境自己进行基准测试(硬件、操作系统、JVM 版本)。

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

由于每个 deque 也是一个队列,因此通常可以说 deque 最多与队列一样好。

回答by Dawood ibn Kareem

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.

Deque 是“双端队列”的缩写。使用普通队列,您可以在一端添加东西并从另一端取出它们。使用双端队列,您可以向任一端添加东西,并从任一端取出它们。这使它更加通用;例如,如果您愿意,您可以将其用作堆栈。

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.

在效率方面,它实际上取决于实现。但一般来说,您不会期望双端队列的性能优于队列,因为(单端)队列可以以不允许在“错误”端添加或删除对象的方式实现。而双端队列的任何实现也可以作为队列的实现。