Java implements Stack class using a Vector, why ? Though it seems that Arraylist should have been a better choice. Java wanted Stack to be thread safe or is it mandatory for a Stack in general ( Which I don't think is the case ) to be thread safe or there is any other theory for using a Vector to implement a Stack in Java?
Stack
and Vector
were present in JDK 1.0. ArrayList
wasn't added until JDK 1.2. Stack
's inheritance of Vector
is baked into the API, so it's impossible to change now.
But it's an outdated class, anyways. Fixing it isn't a priority. Stack
's own Javadoc writeup says:
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With