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

Which of the subclasses of the queue is a “regular regular” queue?

+5
source share
2 answers

(1) java.util.Stack is an inherited class from Java 1.0. It precedes the structure of collections for many years, and frankly, an example of terrible design on many fronts. Nothing about this should be so. The main problem is that Stackextends Vector, and since all inheritance in Java is open inheritance, all methods Vectorare also available on Stack. Thus, you can check any position on the stack, add and remove elements from the middle, clear it or do any number of other things that should not be part of the stack abstraction, without translation. Compare this to the use of interfaces Queueor Deque, thanks to which only methods suitable for the stack are available.

(2) , , LinkedList Queue - , , .

+4

" " . , , , Queue. , , , , (, PriorityQueue), .., "".

0

All Articles