Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack extends Vector

Tags:

java

stack

vector

If stacks extends vector does that mean that stacks are syncronized?

Note from Vector Java docs

Unlike the new collection implementations, Vector is synchronized.

like image 328
Marc Rasmussen Avatar asked Dec 20 '12 15:12

Marc Rasmussen


People also ask

Does stack extend Vector?

The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack.

Is stack a subclass of Vector?

Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own.

What is Vector and stack?

Stack is basically a special case of vector. Theoretically speaking vector can grow as you wish. You can remove elements at any index in a vector. However, in case of a stack you can remove elements and insert them only at its top (hence a special case of vector).

Is stack deprecated in Java?

They are obsolete, but they are not deprecated.


1 Answers

Yes, it is synchronized, but according to the Javadocs you should prefer a Deque instead of a Stack.

From the Stack Javadocs:

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>();

like image 96
dogbane Avatar answered Oct 02 '22 03:10

dogbane