ArrayList vs Vector

F

Flip

I remember reading a while back about the hazards of using Vector for a
dynamic array. However, in the latest javadocs, I've been reading that
Vector has been "retrofitted" for the Collections and is also synchronized.
Does Vector still have a bad rap? Should I be looking at the ArrayList
instead all the time and ditch Vector? When is a good time to use one over
the other? What are your design questions/decisions? As far as I can tell,
they are pretty much equal, but I'm just learning, so I'm asking what you
people think. Thanks.
 
S

Stefan Waldmann

Flip said:
I remember reading a while back about the hazards of using Vector for a
dynamic array. However, in the latest javadocs, I've been reading that
Vector has been "retrofitted" for the Collections and is also synchronized.
Does Vector still have a bad rap? Should I be looking at the ArrayList
instead all the time and ditch Vector? When is a good time to use one over
the other? What are your design questions/decisions? As far as I can tell,
they are pretty much equal, but I'm just learning, so I'm asking what you
people think. Thanks.

Hi!

What exactly did you read about using a Vector as dynamic array being
hazardous??

By sun's API documentation, Vector is synchronized while ArrayList
isn't. If you need a dynamic "array" as class variable in a
multithreaded environment, you should use Vector, or something like

List myList = java.util.Collections.synchronizedList(new ArrayList());

If you don't use Threads or declare your List object in method scope (or
similar), you can use the unsynchronized ArrayList, which is more
performant than Vector/synchronized Lists because the overhead caused by
the synchronizing isn't there.

HTH.
Bye, Stefan


stefan DOT waldmann AT web DOT de
 
R

Roedy Green

I remember reading a while back about the hazards of using Vector for a
dynamic array.

Vector used to be considerably slower, but the JVM thread overhead has
improved so the difference is far less noticeable. Choose Vector for
legacy (MS JVM), or for multithread, otherwise use ArrayList. If you
use the List interface for your references, you can change your mind
later with minimal effort. Just change the constructor.
 
F

Flip

What exactly did you read about using a Vector as dynamic array being
hazardous??
Maybe it was in an older version? Unfortunately I don't have any links to
put here :< maybe it was more word of mouth that Vector was to be avoided.
List myList = java.util.Collections.synchronizedList(new ArrayList());
That works for me.

Thanks for the info! Much appreciated.
 
J

John C. Bollinger

Flip said:
I remember reading a while back about the hazards of using Vector for a
dynamic array. However, in the latest javadocs, I've been reading that
Vector has been "retrofitted" for the Collections and is also synchronized.
Does Vector still have a bad rap? Should I be looking at the ArrayList
instead all the time and ditch Vector? When is a good time to use one over
the other? What are your design questions/decisions? As far as I can tell,
they are pretty much equal, but I'm just learning, so I'm asking what you
people think. Thanks.

Choose ArrayList unless you need the code to run in a 1.1 JVM. Period.

All of Vector's methods are synchronized and thus Vector might seem to
be a natural choice for multithreaded scenarios, but that is a trap.
There are a number of fairly routine usage scenarios that require
higher-level synchronization (e.g. iterating through the list), and
programmers who make a blanket assumption that Vector is safe for
multithreaded scenarios run into trouble. If you need to share a List
of some flavor among multiple threads then you must always analyze the
usage and synchronize appropriately. As long as you're going to do that
anyway, Vector doesn't really offer any advantage. You may also be able
to use fewer total monitors for synchronization (e.g. if there are other
shared objects you need to protect as well) with an ArrayList than with
a Vector. Plus, the explicit synchronization that you put in the source
code to protect an ArrayList clues in future maintainers to be thread-aware.


John Bollinger
(e-mail address removed)
 
C

Chris Smith

John said:
Plus, the explicit synchronization that you put in the source
code to protect an ArrayList clues in future maintainers to be thread-aware.

It's that last bit that's important; the same synchronization behavior
from Vector is available at little extra trouble for ArrayList simply by
wrapping the list in a call to Collections.synchronizedList(...). The
difference is that when you use Vector, future maintainers are likely to
think that Vector is used because it's older. Using
Collections.synchronizedList(...) makes it clear that you feel the need
for that synchronization and intentionally put it in, as opposed to
inheriting it as an unimportant implementation quirk.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Dale King

Roedy Green said:
Vector used to be considerably slower, but the JVM thread overhead has
improved so the difference is far less noticeable. Choose Vector for
legacy (MS JVM), or for multithread, otherwise use ArrayList. If you
use the List interface for your references, you can change your mind
later with minimal effort. Just change the constructor.

Multithreading is not a valid reason to use Vector since you can do:

List myList = java.util.Collections.synchronizedList(new ArrayList());

Supporting legacy is really the only reason to choose Vector.
 
T

Tony Morris

Flip said:
I remember reading a while back about the hazards of using Vector for a
dynamic array. However, in the latest javadocs, I've been reading that
Vector has been "retrofitted" for the Collections and is also synchronized.
Does Vector still have a bad rap? Should I be looking at the ArrayList
instead all the time and ditch Vector? When is a good time to use one over
the other? What are your design questions/decisions? As far as I can tell,
they are pretty much equal, but I'm just learning, so I'm asking what you
people think. Thanks.

A java.util.Vector should (speaking from a purist point of view) only be
used if you wish to support VMs prior to 1.2.
The same can be said for java.util.Hashtable.

If you need thread-safety you synchronize your java.util.List using
java.util.Collections.
This is the *nice* way of doing it.

There is no advantage to doing:
java.util.List l = new java.util.Vector();
over
java.util.List l = new java.util.Collections.synchronizedList(new
java.util.ArrayList());

, since the above code requires a VM version >= 1.2.
One might argue (again, purist point of view) that there is a disadvantage
by using the former approach.
The disadvantage being that java.util.Vector has been "retrofitted" and
therefore, contains unnecessary "clutter", which hinders readability.

Performance benchmarks in different environments show results in both
directions, which (in my opinion), voids the argument that one way
outperforms the other.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
T

Tony Morris

Vector used to be considerably slower, but the JVM thread overhead has
improved so the difference is far less noticeable.

Specifically, acquiring a lock has considerably less overhead (than it once
did) if there is no contention for the lock.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
C

Chris Uppal

Tony said:
Specifically, acquiring a lock has considerably less overhead (than it
once did) if there is no contention for the lock.

....and there has never *been* any contention.

At least my understanding is that neither Sun's nor IBM's thin lock
implementations ever deflate a lock after it has been inflated. I am willing
to be corrected ;-)

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top