aspose file tools*
The moose likes Java in General and the fly likes Collections - ArrayList and Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections - ArrayList and Vector" Watch "Collections - ArrayList and Vector" New topic
Author

Collections - ArrayList and Vector

Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
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!
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
ArrayList is a lot quicker, because it is not synchronized.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
David Lu
Greenhorn

Joined: Aug 01, 2000
Posts: 10
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.
John Zukowski
Author
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
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>
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Results of timed tests using different containers (from Thinking in Java):
Type Get Iteration Insert Remove
array 1430 3850 na na
ArrayList 3070 12200 500 46850
LinkedList 16320 9110 110 60
Vector 4890 16250 550 46850

As you can see, Vector is slower in every category.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
There is one other thing that this chart clearly shows... if you can, use an array!!!
John Zukowski
Author
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
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"
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
jQuery in Action, 2nd edition
 
subject: Collections - ArrayList and Vector
 
Similar Threads
Unreferenced Interface,
collections framework..
ArrayList Vector Collections Array
convert collections to string
Vector deprecated in 1.3?