[Jeroen]: Vector is also very very slow compared to other Collections.
Not in my experience. It's very very pointless to use for any reason other than compatibility with some archaic API, but the performance is pretty much comparable to ArrayList. The synchronization issue makes remarkably little difference if you're not actually using multiple threads.
[Matt]: Vector is single-threaded (i.e., synchronized)
Vector is synchronized, sure. (Rather pointlessly I may add.) But since when does that mean "single-threaded"? To me a single-threaded application only has one thread, period, while a synchronized class may allow many different threads to access it - but only one at a time. (Assuming the most common scenario, all methods synchronized on the same monitor.) To me those are very different situations, and I wouldn't want to see them conflated. I would say Vector is synchronized, but not single-threaded. (Unless you use it in a single-threaded application.)
Instead of strings if you use Vectors ,they automatically expand themselves. If you want to display the values dynamically ,you can use vectors.
Jeroen T Wenting
posted 12 years ago
Jim, I've timed Vector against ArrayList and found that the same method processing the same dataset (several thousand records) was an order of magnitude faster when using ArrayList than it was using Vector (milliseconds rather than seconds).
Originally posted by Jeroen T Wenting: Jim, I've timed Vector against ArrayList and found that the same method processing the same dataset (several thousand records) was an order of magnitude faster when using ArrayList than it was using Vector (milliseconds rather than seconds).
I'd hardly call that an insignificant difference.
that depends on a lot of things, including your operating system, which version of the JVM you're running, etc.
Not that I disagree with you. Array list is faster because it doesn't have the overhead of synchronization. However, the point that you are arguing on is one that varies from machine to machine, so while I'm not saying you are wrong, I also feel that Jim is just as correct. It's certainly not so horrendously slow that it would turn a complicated algorithm performed on each element of a list into an massively slow operation. If the algorithm is even slightly complex, the time to perform the computations on each element of the list is so much greater than the access time that the synchronization overhead is essentially negligible. If it takes 200 times as long to perform the calculations as it does to get the access the element of the list, then who cares about a magnitude difference in the access time? You've just increased total run time by, what, 5%, maybe? Is it really that important? Sometimes, yes, sometimes, no.
Furthermore, in my experience, in many (but admittedly not all) cases where you are really concerned about speed of accessing data in a collection, you will want to be using the fastest jvm available, in which case, you don't really have to worry about the synchronization cost (for example, in 1.5, it can almost be ignored in most cases). Also, for situations where you ARE using multithreading, you would have to synchronize your array list as well, so there, the point is basically moot.
Anyway, my vote is for ArrayList, but I wouldn't say that vector is EXTREMELY slower that ArrayList in all, or even most, situations.
posted 12 years ago
Originally posted by preeti math: Instead of strings if you use Vectors ,they automatically expand themselves. If you want to display the values dynamically ,you can use vectors.
See, to me, you're comparing apples and oranges. Sure, a Vector could be used to store Character objects, but really, if you're looking for an expandable character array class, wouldn't StringBuffer of StringBuilder be more appropriate (not to mention, more optimized to that specific task)?