File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What's diff btw ArrayList and Vector? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What Watch "What New topic
Author

What's diff btw ArrayList and Vector?

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30377
    
150

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
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.


Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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

Joined: Sep 29, 2000
Posts: 8521
Moving to the Java in General Intermediate forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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);


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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

Joined: Jun 02, 2003
Posts: 1923

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.
 
 
subject: What's diff btw ArrayList and Vector?