开发者

In Java, why is it that a Stack is a concrete class whereas the Queue is an interface?

开发者 https://www.devze.com 2023-02-21 16:34 出处:网络
开发者_开发知识库Which one of Queue\'s subclasses is a \'plain ordinary\' queue?(1) java.util.Stack is a legacy class from Java 1.0. It predates the Collections framework by many years, and it\'s fran

开发者_开发知识库Which one of Queue's subclasses is a 'plain ordinary' queue?


(1) java.util.Stack is a legacy class from Java 1.0. It predates the Collections framework by many years, and it's frankly an example of horrible design on many fronts. Nothing about it is the way things should be. The main problem is that Stack extends Vector, and as all inheritance in Java is public inheritance, all the methods of Vector are available on Stack as well. Therefore, you can inspect any position in a Stack, add and remove elements from the middle, clear it, or do any number of other things that should not be part of a stack abstraction, without a cast. Contrast that to using the Queue or Deque interfaces, through which only stack-appropriate methods are available.

(2) There really is no such thing as a plain ordinary queue, but LinkedList implements Queue without any special semantics, so maybe that's what you want.


Any of its concrete subclasses can be used as a "plain ordinary" queue. True, they may have additional functionality, but you're free to use only the methods declared in the Queue interface. Though I imagine you might want to avoid the subclasses whose extra functionality affect queue ordering (like PriorityQueue), capacity, etc. if you want an "ordinary" one.

0

精彩评论

暂无评论...
验证码 换一张
取 消