java.util.queue 的实现如何使用 LIFO?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6833140/
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
How implementation of java.util.queue uses LIFO?
提问by celsowm
In Java doc:
在 Java 文档中:
[...] Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out)
[...] 其中的例外是优先级队列,它根据提供的比较器或元素的自然顺序对元素进行排序,以及对元素进行 LIFO(后进先出)排序的 LIFO 队列(或堆栈)
How implementation of java.util.queue
uses LIFOinstead of FIFO?
如何实现java.util.queue
使用LIFO而不是FIFO?
采纳答案by AlexR
Stack and LinkedList offered here are just a collections. Queue is not a collection. It is a part of concurrency package and can be used with threadpools.
这里提供的 Stack 和 LinkedList 只是一个集合。队列不是集合。它是并发包的一部分,可以与线程池一起使用。
I have just verified again and read javadoc that you have quoted. I think that the only option to use LIFO queue is to use priority queue with custom comparator that compare elements according to the insertion time in reverse order.
我刚刚再次验证并阅读了您引用的 javadoc。我认为使用 LIFO 队列的唯一选择是使用优先级队列和自定义比较器,根据插入时间以相反的顺序比较元素。
回答by ZhekaKozlov
You can use any Deque as a LIFO queue using Collections.asLifoQueue method:
您可以使用 Collections.asLifoQueue 方法将任何 Deque 用作 LIFO 队列:
Queue<Integer> arrayLifoQueue = Collections.asLifoQueue(new ArrayDeque<Integer>());
Queue<Integer> linkedListLifoQueue = Collections.asLifoQueue(new LinkedList<Integer>());
回答by Oscar Gomez
You can use a java.util.LinkedList
and use the pop()
and push()
methods and use it like a stack, which is a LIFO queue.
您可以使用 ajava.util.LinkedList
和pop()
和push()
方法并将其用作堆栈,这是一个 LIFO 队列。
回答by dan
Deque can be used as LIFO or FIFO
Deque 可用作 LIFO 或 FIFO
回答by Adrian Malik
Implementation of the Queuecan base on FIFO, prioritiesand LIFO- that is what official documentation says.
队列的实现可以基于FIFO、优先级和LIFO- 这就是官方文档所说的。
When a programmer first sees "Queue" he automatically thinks "it must be FIFO order" (or eventually prioritized order). But as documentation says there must be possibility to use Queue interface for LIFO ordering. Let me explain you how it can be done.
当程序员第一次看到“队列”时,他会自动认为“它一定是 FIFO 顺序”(或最终优先顺序)。但正如文档所说,必须有可能使用 Queue 接口进行 LIFO 排序。让我向您解释如何做到这一点。
// 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, Queue interface can be used also as a LIFO.
正如您所看到的,根据实现的不同,Queue 接口也可以用作 LIFO。
回答by sgokhales
Queue is a data structure that uses a technique of First-In-First-Out.
队列是一种使用先进先出技术的数据结构。
Here's a useful link : magi.toolkit.util.queue Class LIFOQueue
这是一个有用的链接:magi.toolkit.util.queue Class LIFOQueue
An implementation of a "Last In, First Out" Queue. Basically, a LIFO Queue is a Stack.
“后进先出”队列的实现。基本上,LIFO 队列是一个堆栈。
回答by ulyssis2
I made a LIFO queue with limited size. Limited size is maintained by replacing the oldest entries with the new ones. The implementation is based on LinkedList.
我制作了一个大小有限的 LIFO 队列。通过用新条目替换最旧的条目来维持有限的大小。实现基于 LinkedList。
package XXXX;
import java.util.LinkedList;
public class LIFOQueueLimitedSize<E> extends LinkedList<E> {
/**
* generated serial number
*/
private static final long serialVersionUID = -7772085623838075506L;
// Size of the queue
private int size;
// Constructor
public LIFOQueueLimitedSize(int crunchifySize) {
// Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy
this.size = crunchifySize;
}
// If queue is full, it will remove oldest/first element from queue like FIFO
@Override
synchronized public boolean add(E e) {
// Check if queue full already?
if (super.size() == this.size) {
// remove element from queue if queue is full
this.remove();
}
return super.add(e);
}
}