Martin Vajsar wrote:I still strongly prefer ArrayList over Vector.
Firstly, Collections.synchronizedList(new ArrayList()); does not look like an unreasonable amount of effort to me.
Secondly, when someone spots this construction, it is immediately clear the ArrayList is intentionally synchronized and is therefore going to be used by multiple threads. If I used a Vector instead, the developer to come after me might not know for sure whether I had a bout of nostalgia, or I really use Vector just to get synchronized access.
For me, Vector equals legacy code.
Yeah, I stand corrected. Vectors are pre 1.2 and shouldn't really be used with the newer collections in the Java API.
I wonder why it's not deprecated?
Here is a quote from Jon Skeet explaining (
http://stackoverflow.com/a/1386288/603732):
Jon Skeet wrote:
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) 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 MrSpandex 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.