Why is Java Vector (and Stack) class considered obsolete or deprecated?
Vector
synchronizes on each individual operation. That's almost never what you want to do.
Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector
, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException
in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?
Of course, it also has the overhead of locking even when you don't need to.
Basically, it's a very flawed approach to synchronization in most situations. As Mr Brian Henk pointed out, you can decorate a collection using the calls such as Collections.synchronizedList
- the fact that Vector
combines both the "resized array" collection implementation with the "synchronize every operation" bit is another example of poor design; the decoration approach gives cleaner separation of concerns.
As for a Stack
equivalent - I'd look at Deque
/ArrayDeque
to start with.
Why is java.util.Stack not marked as deprecated?
The Javadoc class documentation for java.util.Stack as per Java SE 14 does come with this guidance:
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>();
Vector is an obsolete Collection
First of all, although Vector
is mostly obsoleted by ArrayList
, it is still perfectly legal to use, and your project should run just fine.
As noted, however, it is not recommended to use. The main reason for this is that all of its methods are synchronized
, which is usually useless and could considerably slow down your application. Any local variable that's not shared outside the scope of the method can safely be replaced with an ArrayList
. Method arguments, return values and data members should be inspected closely before being replaced with ArrayList
, lest you unwittingly change the synchronization semantics and introduce a hard-to-discover bug.
Java 6 SE are vectors obsolete?
Use of Vector
s have been discouraged for some time now. They are replaced by ArrayList
, which has more or less the same functionality (but isn't synchronised).
Why to use a Deque instead of inbuilt Stack?
Java generics were added after initial implementations of collections; Stack
is from Java 1.0 - and rather then break existing code when they added generics, it was decided to add classes that duplicate functionality (but provide a consistent API). That is why you should prefer a Deque
- it provides an API consistent with all of the other Java Collection
s.
Should Vector be deprecated?
While it is rarely a good idea to use Vector
in new code, there is no pressing need to deprecate. While the new collections classes are superior, using the Vector
class doesn't actually break anything.
Furthermore, there are a number of other standard Java APIs that depend on the Vector
API, and there are doubtless hundreds of thousands of customer and third party applications that use it as well.
Basically, deprecating Vector
would be unnecessarily disruptive. There is no need to push people into changing code that works reliably, if marginally slower.
It has been suggested / implied that they could deprecate Vector
without (ever) actually removing it. But the problem is that deprecation warnings create real work for real people. If this is done unnecessarily, busy people will start suppressing the warnings by default. (Recall the story of the boy who cried wolf .... )
Related Topics
Why Is It Considered a Bad Practice to Omit Curly Braces
How to Return a Documentsnapshot as a Result of a Method
How to Use an Existing Database With an Android Application
How to Check Internet Access on Android? Inetaddress Never Times Out
What Is a Stringindexoutofboundsexception - How to Fix It
How to Discover Memory Usage of My Application in Android
Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X0
Is There a Java Equivalent or Methodology For the Typedef Keyword in C++
Remove HTML Tags from a String
What Are the Pros and Cons of the Leading Java HTML Parsers
How to Efficiently Parse HTML With Java
Inconsistent Performance Applying Foregroundactions in a Jeditorpane When Reading Html
Convert Word Doc to HTML Programmatically in Java
How to Style the Progressbar Component in Javafx