Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

java - 为什么要在堆栈上使用Deque?(Why should I use Deque over Stack?)

I need a Stack data structure for my use case.

(我的用例需要一个Stack数据结构。)

I should be able to push items into the data structure and I only want to retrieve the last item from the Stack.

(我应该能够将项目推送到数据结构中,而我只想从堆栈中检索最后一个项目。)

The JavaDoc for Stack says :

(JavaDoc for 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.

(Deque接口及其实现提供了一组更完整和一致的LIFO堆栈操作,应优先使用此类。)

For example:

(例如:)

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

I definitely do not want synchronized behavior here as I will be using this datastructure local to a method .

(我绝对不希望这里出现同步行为,因为我将使用方法本地的此数据结构。)

Apart from this why should I prefer Deque over Stack here ?

(除此之外,为什么在这里我应该更喜欢Deque不是Stack ?)

PS: The javadoc from Deque says :

(PS:Deque的Javadoc说:)

Deques can also be used as LIFO (Last-In-First-Out) stacks.

(双端队列也可以用作LIFO(后进先出)堆栈。)

This interface should be used in preference to the legacy Stack class.

(此接口应优先于旧版Stack类使用。)

  ask by Geek translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For one thing, it's more sensible in terms of inheritance.

(一方面,在继承方面更明智。)

The fact that Stack extends Vector is really strange, in my view.

(在我看来, Stack扩展Vector的事实确实很奇怪。)

Early in Java, inheritance was overused IMO - Properties being another example.

(在Java早期,继承被IMO过度使用- Properties是另一个示例。)

For me, the crucial word in the docs you quoted is consistent .

(对我而言,您引用的文档中的关键词是一致的 。)

Deque exposes a set of operations which is all about being able to fetch/add/remove items from the start or end of a collection, iterate etc - and that's it.

(Deque公开了一组操作,这些操作都与从集合的开始或结束取回,添加/删除项,进行迭代等有关,仅此而已。)

There's deliberately no way to access an element by position, which Stack exposes because it's a subclass of Vector .

(故意没有办法按位置访问元素,这是Stack公开的, 因为它是Vector的子类。)

Oh, and also Stack has no interface, so if you know you need Stack operations you end up committing to a specific concrete class, which isn't usually a good idea.

(哦,而且Stack也没有接口,所以如果您知道需要Stack操作,您最终会提交给特定的具体类,这通常不是一个好主意。)

Also as pointed out in the comments, Stack and Deque have reverse iteration orders:

(同样如注释中所指出的, StackDeque具有反向迭代顺序:)

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(new ArrayList<>(stack)); // prints 1, 2, 3


Deque<Integer> deque = new ArrayDeque<>();
deque.push(1);
deque.push(2);
deque.push(3);
System.out.println(new ArrayList<>(deque)); // prints 3, 2, 1

which is also explained in the JavaDocs for Deque.iterator() :

(JavaDocs for Deque.iterator()也对此进行了说明:)

Returns an iterator over the elements in this deque in proper sequence.

(以适当的顺序返回此双端队列中的元素的迭代器。)

The elements will be returned in order from first (head) to last (tail).

(元素将按照从头(头)到后(尾)的顺序返回。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...