Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections - ArrayList and Vector

 
Aj Manch
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList is a lot quicker, because it is not synchronized.
 
David Lu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one other thing that this chart clearly shows... if you can, use an array!!!
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic