gpt4 book ai didi

java - 从 Collections.asLifoQueue(Deque) 和 Stack (Java 6) 返回的队列之间的区别

转载 作者:行者123 更新时间:2023-11-29 08:54:11 25 4
gpt4 key购买 nike

作为 java 6 的一部分,在 Collections 实用程序类中添加了一个 asLifoQueue(Deque) 静态方法。

Javadoc 说

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

Returns a view of a Deque as a Last-in-first-out (Lifo) Queue. Method add is mapped to push, remove is mapped to pop and so on. This view can be useful when you would like to use a method requiring a Queue but you need Lifo ordering.

Each method invocation on the queue returned by this method results in exactly one method invocation on the backing deque, with one exception. The addAll method is implemented as a sequence of addFirst invocations on the backing deque.

现在这个和stack有什么区别。这本质上不是一个堆栈吗?

最佳答案

This view can be useful when you would like to use a method requiring a Queue but you need Lifo ordering.

该方法只是包装了 deque 参数,因此您可以处理 Queue 实现。正如你所说,队列只是一个堆栈,但是 javadoc Stack 类说:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:

    Deque<Integer> stack = new ArrayDeque<Integer>();

Deque 只是比 Stack 更完整,方法 asLifoQueue 只是一个将双端队列转换为类似堆栈的数据结构的实用程序.

关于java - 从 Collections.asLifoQueue(Deque) 和 Stack (Java 6) 返回的队列之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21183797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com