Question to John Zukowski: The Java SDK1.3 API docs mention that Vector is synchronised and ArrayList is not. I think that's a plus for Vector. Also, I couldn't find any documentation that prove that ArrayList is better than Vector. So, my question - what are the advantages of using ArrayList over Vector other than that ArrayList is a member of Collections framework and Vector has been retrofitted to be one. Thanks!
I agree with Frank. If your code is not multi-threaded and there is no need to have synchronization, use the ArrayList! It is A LOT faster. Aside from synchronization, the only benefit I've found to using a Vector is that many components such as JList, JComboBox, and JTable can take a vector of data in the Constructor to initialize the component. However, if you are using an ArrayList, these components can also take an array of Objects which can be obtained from the ArrayList using the toArray() method.
The key benefit of ArrayList over Vector is that is isn't synchronized <em>if you don't need the synchronization</em>. Yes, Vector is slightly faster if you do need the synchronization, as it doesn't have an extra level of method redirection through the wrapper. ------------------ John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
John Zukowski Author of <a href="http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="http://www.amazon.com/exec/obidos/ASIN/1893115925/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1893115984/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
There is one other thing that this chart clearly shows... if you can, use an array!!!
Joined: Mar 29, 2001
The one number that surprised me there is the iteration. LinkedList is faster than both Vector and ArrayList. And yes, arrays are definitely better, IF you don't need the dynamic sizing. ------------------ John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
Originally posted by John Zukowski: The key benefit of ArrayList over Vector is that is isn't synchronized <em>if you don't need the synchronization</em>.
Even if you do need synchronization, the Vector itself is often the wrong place. Say, you need to retrieve an object from the Vector, creating and inserting it if it's not already there. Logic like this generally needs to be protected by a synchronized block even though the Vector itself is already synchronized. The synchronization inside the vector is, in that case, just a waste of CPU cycles. Maybe it's just me. I never feel that I'm getting a lot of mileage out of synchronized objects, and much prefer the Collections concept of using a synchronizing wrapper in the rare cases that that is exactly what you're looking for. - Peter
Strong agreement with Peter here. I think that the synchronization in Vector provides a false sense of security for many programmers, who may not realize that they often will need synchronization at other levels as well in order to be thread-safe. I think that using explicit synchronized() blocks where necessary encourages other coders to take note of just how the synchronization is being achieved, rather than brushing the details under the rug. > The synchronization inside the vector is, in that case, just > a waste of CPU cycles. Yup. Although I'd also like to note, it's not much of a waste. Discussions of how to improve Java performance often emphasize the need to avoid unneccessary synchronization, because it takes time to acquire a lock if a thread doesn't have it - especially if the lock is currently held by another thread, forcing your thread to wait. However, if your thread already has the required lock, it's not much of a delay to "reacquire" it (i.e. make the JVM verify that the lock is already held). It's unnecessary, true, by usually not a big problem performance-wise.