Java 集合中的 Queue 类在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/804078/
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
Where is the Queue class in the Java Collections?
提问by
I only see a Queue interface, is there no Queue class in the Java Collections?
我只看到一个 Queue 接口,Java Collections 中没有 Queue 类吗?
回答by Rob
回答by Michael Myers
The Javadocsgive a list of classes which implement Queue
.
该Javadoc中给出其实现类的列表Queue
。
All Known Implementing Classes:
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedBlockingDeque, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue
所有已知的实现类:
AbstractQueue,ArrayBlockingQueue,ArrayDeque,的ConcurrentLinkedQueue,DelayQueue,的LinkedBlockingQueue,LinkedBlockingDeque,链表,的PriorityBlockingQueue,PriorityQueue中,的SynchronousQueue
There are also some subinterfaces which you might find useful:
还有一些您可能会发现有用的子接口:
All Known Subinterfaces:
BlockingDeque<E>, BlockingQueue<E>, Deque<E>
所有已知的子接口:
BlockingDeque<E>、BlockingQueue<E>、Deque<E>
回答by akappa
Queue has multiple implementations: from the API:
Queue 有多种实现:来自 API:
All Known Implementing Classes:
所有已知的实现类:
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue,
DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList,
PriorityBlockingQueue, PriorityQueue, SynchronousQueue
Note that AbstractQueue isn't a concrete class.
请注意, AbstractQueue 不是一个具体的类。
Some of these are from the package concurrent, so if you're implementing a jobqueue or something similar, you should go for ConcurrentLinkedQueue or PriorityBlockingQueue (for an heap) for ex.
其中一些来自包 concurrent,所以如果你正在实现一个 jobqueue 或类似的东西,你应该选择 ConcurrentLinkedQueue 或 PriorityBlockingQueue(对于堆)。
回答by Dave Costa
http://java.sun.com/javase/6/docs/api/java/util/Queue.html-- see section "All Known Implementing Classes". There are a variety of implementations which are suitable for different purposes.
http://java.sun.com/javase/6/docs/api/java/util/Queue.html- 请参阅“所有已知的实现类”部分。有多种实现方式适用于不同的目的。
回答by Tom Hawtin - tackline
As well as using the API docs to find "all known implementing classes", there are often other non-public implementation that are nevertheless available through the public API (only without requiring reams of pointless documentation). If you click on "use" you will also find Collections.asLifoQueue(Deque
is already a Queue
, but it is FIFO rather than a stack).
除了使用 API 文档查找“所有已知的实现类”外,通常还有其他非公共实现可通过公共 API 获得(只是不需要大量无意义的文档)。如果单击“使用”,您还会发现Collections.asLifoQueue(Deque
已经是一个Queue
,但它是 FIFO 而不是堆栈)。
回答by Bill K
Although the answers sound kind of scornful, They are actually being pretty cool by teaching you how to fish. A Queue is simply a way to look at a collection, so many collections may implement it. As well, things that act like collections but with specific other logic (like thread queues) might use the same interface.
虽然这些答案听起来有点轻蔑,但他们教你如何钓鱼实际上很酷。队列只是查看集合的一种方式,因此许多集合都可以实现它。同样,行为类似于集合但具有特定其他逻辑(如线程队列)的事物可能使用相同的接口。
Knowing where to look at the javadocs is a big help. I'm sure you looked but just didn't think to look at the implementations. Live and learn.
知道在哪里查看 javadoc 很有帮助。我相信你看过但只是没想到看实现。活到老,学到老。
Sometimes you may also have to chase down sub-class/extends lists. Like if you looked at Queue and saw AbstractQueue, you might want to see what classes implement that.
有时您可能还需要追踪子类/扩展列表。就像您查看 Queue 并看到 AbstractQueue 一样,您可能想查看哪些类实现了它。
I'll get rid of one of your -1s for ya :)
我会为你摆脱你的 -1 之一:)
回答by FoOzA
import java.util.Queue;
just that
只是
Enqueue function == Queue_Object.add(input_value);
Dequeue function == Queue_Object.pull(); //return the value and delete it from queue
回答by Injektilo
No, there is no Queue
class, because there are lots of different ways to implement a queue and you have to pick the one that suits your use case. The same goes for any of the other collections in the collections framework - for example, ArrayList
and LinkedList
both implement a List
. The general pattern, which is a good use of object inheritance, is:
不,没有Queue
类,因为实现队列有很多不同的方法,您必须选择适合您的用例的方法。这同样适用于任何在集合框架中的其他收藏品-例如,ArrayList
并且LinkedList
都实现List
。很好地利用了对象继承的一般模式是:
The Interface, e.g. Queue
, defines the role you want an object to play;
该接口,例如Queue
,定义你想要的目的是发挥的作用;
Sub-interfaces, e.g. Deque
, further expands on or specialises the role - in this case a "deque" or double-ended queue allows you to add and remove elements from both ends of the queue, as opposed to only adding to the back and removing from the front;
子接口,例如Deque
,进一步扩展或专门化角色 - 在这种情况下,“双端队列”或双端队列允许您从队列的两端添加和删除元素,而不是仅添加到后面和删除从前面;
Classesprovide the implementation of how an object carries out the role. For example, an ArrayDeque
uses a resizable array to implement a double-ended queue, which has different strengths and weaknesses to LinkedList
which uses a linked list.
类提供对象如何执行角色的实现。例如,一个ArrayDeque
使用可调整大小的数组来实现一个双端队列,它与LinkedList
使用链表的优点和缺点各不相同。
To elaborate on the idea of an interface as a role, note that even though ArrayDeque
implements Deque
, you can use it as a Queue
without having to worry about that because implementing both interfaces means it can play both roles. Similarly, LinkedList
can wear a List
, Queue
or Deque
hat.
要详细说明接口作为角色的想法,请注意,即使ArrayDeque
实现Deque
,您也可以将其用作 ,Queue
而不必担心,因为实现两个接口意味着它可以同时扮演两个角色。同样,LinkedList
可以戴一顶List
,Queue
或Deque
帽子。
For this reason, the normal (recommended) way to use something like the Collections framework is to program to the interface, that is, use an interface when using the class rather than the class name itself. For example, you would instantiate an object like this:
出于这个原因,使用类似 Collections 框架的东西的正常(推荐)方法是对接口进行编程,即在使用类时使用接口而不是类名本身。例如,您可以像这样实例化一个对象:
Queue<String> logQueue = new ConcurrentLinkedQueue<String>();
...
logQueue.add("Log message");
In this way you are
这样你就
- not tied to a particular class and can use a drop-in replacement if needed without having to modify much code, and
- are documenting what you are doing with a class by naming the role it plays. The general principle this helps with is self-documenting code, which is essentially to let the code itself be self-explanatory without having to use comments, etc.
- 不绑定到特定的类,并且可以在需要时使用直接替换而无需修改大量代码,并且
- 通过命名它所扮演的角色来记录你对一个类所做的事情。这有帮助的一般原则是自记录代码,它本质上是让代码本身不言自明,而不必使用注释等。
回答by developer747
Queue<Integer> queue = new ArrayDeque<>();
queue.add(1);
queue.add(2);
queue.add(3);
while (!queue.isEmpty()) {
System.out.println(queue.remove());// prints 1 2 3
}
You can also use a LinkedList. But generally, for a Queue, ArrayDeque is preferred over LinkedList. Because ArrayDeque consumes lesser memory, is faster and doesn't allow nulls. Not allowing null is good, because if you do allow nulls, then when you do a peek() or poll() you could get a null even if the queue is not empty.
您还可以使用 LinkedList。但通常,对于 Queue,ArrayDeque 优于 LinkedList。因为 ArrayDeque 消耗更少的内存,速度更快并且不允许空值。不允许空值是好的,因为如果你确实允许空值,那么当你执行 peek() 或 poll() 时,即使队列不为空,你也可以获得空值。