• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's diff btw ArrayList and Vector?

 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know Vector is synchronized and ArrayList is not. What does it mean to me? When shall I use Vector? When shall I use ArrayList?
Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jenny,
You would use a Vector when more than one method needs to access the list at the same time. Otherwise, you would use an ArrayList
 
Srikanth Shenoy
author
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you dont care about data safety when more than one thread accesses the collection (may be bcoz the data is read only), you can use ArrayList to get better performance.
Synchronized methods take slightly more time even when there is no contention and time in several orders of magnitude when there is contention to acquire the monitor.
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jeanne and Srikanth!
So if I'm using them in Struts Action, then I'd better use ArrayList, right? Because every invocation of Action class is a new thread?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Java in General Intermediate forum.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jenny Yin:
Thanks, Jeanne and Srikanth!
So if I'm using them in Struts Action, then I'd better use ArrayList, right? Because every invocation of Action class is a new thread?

Isn't it all the all you should be using Vector? Since different threads may be accessing/attempting to modify your data at the same time?
Threads aren't synchronized.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait. You should never use Vector. It was left in the language only for classes that require a Vector as input to a method. If you need a synchronized Collection object then use one of the synchronized methods of the Collections class.
For example, if you need a synchronized List object use:
List myList = new ArrayList();
List mySynchList = Collections.synchronizedList(myList);
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you shouldn't use Vector, why isn't it deprecated?
I'm using Vector and have a lot of fun with it:
- It works.
- It's faster to type than ArrayList.
- It's fine, when performance isn't an issue.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
If you shouldn't use Vector, why isn't it deprecated?
I'm using Vector and have a lot of fun with it:
- It works.
- It's faster to type than ArrayList.
- It's fine, when performance isn't an issue.

It's not deprecated because so many classes that are part of J2SE rely on it and they have no intention of rewriting half of the Java API.
The main problem with Vector is that it is not a true member of the Collection family as it has been retrofitted in. It still has a whole series of methods that perform the same function as the Collection methods but are not compatible with other members of the Collection family. There is simply no reason to use Vector ever. When Joshua Bloch rewrote the collection framework he identified Vector and Hashtable as legacy classes that should not be used excpet when required to pass data to an xisting method. I think Joshua Bloch's advice is well worth taking.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - that sounds much less dogmatic than 'You should never use Vector'.
And you make some unintendet arguments for the Vector-class.
There is a lot of code using old-fashion awt methods in applets, and perhaps a lot of code using old Threads.
But the problematic code is marked deprecated.
If Vector would be a serious problem, it should have been marked deprecated as soon as possible! Marking it deprecated would not mean it will not work as you know. And the more old code uses Vector, the more important it would be to mark it deprecated.
If you are a beginner, and don't know the difference between Vector and the alternative collection-classes, you should try to avoid it's usage, to make exchanging of your collection more easy, and not to get used to Vector only.
But in an intermediate forum, we can be a little more precise, than giving rules of thumb.
P.S.: I almost stoped using Vector in new code, but will not change old code using it, until necessary.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic