Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java.util.Stack is implemented using Vector and Not an Arraylist

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?

like image 738
chitresh sirohi Avatar asked Feb 07 '23 02:02

chitresh sirohi


1 Answers

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>();
like image 151
John Kugelman Avatar answered Feb 08 '23 17:02

John Kugelman