10

When we don't need synchronization ArrayList is faster than Vector. And when we do need a synchronized collection we're better off using Synchronization wrappers (correct me if I'm wrong), or synchronizing the code only when there are calls on that collection. Are there cases where using Vector is the best option?

Michael
  • 13,838
  • 18
  • 52
  • 81
  • I could be wrong, but I believe that the HotSpot JVM can determine whether or not synchronization is necessary on a particular object and optimize it away if it is not necessary. For example, years ago, the `StringBuilder` class was introduced as a non-synchronized alternative to `StringBuffer` (to avoid the performance penalties caused by unnecessary locking). However, HotSpot can now optimize away `StringBuffer`'s synchronizing (if it's deemed unnecessary), effectively allowing its speed to match `StringBuilder`'s. I wonder if `Vector` has also benefited from such optimizations... – Adam Paynter Dec 24 '10 at 10:32
  • Interesting, I'll look that up and repost if I find something. – Michael Dec 24 '10 at 10:34
  • 2
    @Michael: I heard about this from Steve Yegge in his [Dynamic Languages Strike Back](http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html) talk (search for `StringBuffer` in the transcript). He links to an article on (Biased Locking in Java 6)[http://java.sun.com/performance/reference/whitepapers/6_performance.html#2.1.1]. – Adam Paynter Dec 24 '10 at 10:46
  • 1
    @Adam Paynter: It is an optimization (escape-analysis) that has to be activated (-XX:+DoEscapeAnalysis). Maybe it is standard in Java 7. Anyways, I don't believe synchronization makes much performance-effet in most usual programs. – Mnementh Dec 24 '10 at 10:49
  • 2
    Strange, I wouldn't have considered this question "subject and argumentative". Seems like a legitimate thing to know. – Adam Paynter Dec 25 '10 at 11:22
  • 2
    @Adam Paynter: Agreed, I was actually quite interested in the discussion. The question seems objective to me, and asking a reasonable, concrete question that folks in my organization have asked me a number of times. Perhaps the answers offered subjective responses, but that's not the fault of the questioner... – andersoj Dec 26 '10 at 01:46

4 Answers4

17

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 .... )

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

As far as I can tell, using Vector is only for having a core API that depends on it or a pre-1.2 runtime environment. It might also be a comfort thing. Some people just like Vectors because that's what they originally used.

update: Vector is now deprecated as described here

Matt
  • 5,404
  • 3
  • 27
  • 39
  • 2
    If you "originally used" Vector, then either you have been using Java almost since its very beginning, or you've learned it from very old books/tutorials. I suspect that the latter accounts for 90% of current uses of the class. – Michael Borgwardt Dec 24 '10 at 10:49
  • @Michael: There's quite a few people (myself included) who started using Java in 1.0, though I'd hope that most would know to use the newer collection classes. Guess that marks me out as an optimist… – Donal Fellows Dec 24 '10 at 20:38
1

Because there are a lot of programs out there that use Vector. Removing Vector from the jre would mean they would all be broken. ON the other hand use ArrayList or one of the other bundled List implementations instead according to your need.

mP.
  • 18,002
  • 10
  • 71
  • 105
  • 3
    Yes, obviously removing it would create a mess, but @deprecated would just add a lot of warnings. Anyway I'm not expecting anything to be changed in the APIs, just a confirmation that Vector is useless, or that there are cases where it should be used. – Michael Dec 24 '10 at 10:42
0

Check Lock Elision in java, modern JVM implementations (Mustang) are intelligent enough to optimize shared & non shared object access. Synchronization optimizations in Mustang

So, it does not really matters (vector / arraylist with synchronized keyword); but depricating will result in too many warnings on compilation :(

Dhananjay
  • 3,903
  • 2
  • 29
  • 44