For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > when is Vector appropiate?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author when is Vector appropiate?
thufir.hawat@mail.com

2004-07-28, 9:09 pm

I notice some examples on the web use Vectors; here they're pretty
universally derided. If an ArrayList is synchronized how different is it
from a Vector? Treating each as a black box, is the only difference
performance?


thanks,

Thufir Hawat
still pondering generics
Chris Smith

2004-07-28, 9:09 pm

thufir.hawat@mail.com wrote:
> I notice some examples on the web use Vectors; here they're pretty
> universally derided. If an ArrayList is synchronized how different is it
> from a Vector? Treating each as a black box, is the only difference
> performance?


First of all, I will assume you meant "If I synchronize an ArrayList
using Collections.synchronizedList, ...". If you really meant "Since an
ArrayList is synchronized..." then I would feel the need to point out
that there is absolutely no synchronization in ArrayList by default.

Second, yes that's basically it; ArrayList and Vector are functionally
identical. Their performance is not even likely to be all that
different. There are a couple reasons to choose ArrayList over Vector,
though:

1. It's idiomatic. It follows the typical Collections API usage to a T,
whereas Vector was retrofitted. The default synchronized behavior is
one example of a place where Vector was forced to differ with typical
collections because of backward compatibility. It also has the
confusing dual names for methods that do the same thing (addElement vs.
add, for example), and isn't named according to the typical naming
convention of the Collections API.

2. It's more consistent to use ArrayList all the time rather than switch
back and forth depending on your need for synchronized blocks (which is
really the only reason you'd switch). Collections.synchronizedList may
look more complex, but not when you consider that you use almost
identical code to synchronize a Map or Set as well.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
thufir.hawat@mail.com

2004-07-28, 9:09 pm

On Tue, 27 Jul 2004, Chris Smith wrote:
> First of all, I will assume you meant "If I synchronize an ArrayList
> using Collections.synchronizedList, ...". If you really meant "Since an
> ArrayList is synchronized..." then I would feel the need to point out
> that there is absolutely no synchronization in ArrayList by default.


oops, yes I meant *if*, thanks.

> Second, yes that's basically it; ArrayList and Vector are functionally
> identical. Their performance is not even likely to be all that
> different. There are a couple reasons to choose ArrayList over Vector,
> though:
>
> 1. It's idiomatic. It follows the typical Collections API usage to a T,
> whereas Vector was retrofitted. The default synchronized behavior is
> one example of a place where Vector was forced to differ with typical
> collections because of backward compatibility. It also has the
> confusing dual names for methods that do the same thing (addElement vs.
> add, for example), and isn't named according to the typical naming
> convention of the Collections API.
>
> 2. It's more consistent to use ArrayList all the time rather than switch
> back and forth depending on your need for synchronized blocks (which is
> really the only reason you'd switch). Collections.synchronizedList may
> look more complex, but not when you consider that you use almost
> identical code to synchronize a Map or Set as well.
>
> --
> www.designacourse.com
> The Easiest Way to Train Anyone... Anywhere.
>
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation
>


makes sense, thanks.

Thufir Hawat
<http://www.shaw.ca/members/hawat/source/>
Oscar kind

2004-07-28, 9:09 pm

Chris Smith <cdsmith@twu.net> wrote:
> 2. It's more consistent to use ArrayList all the time rather than switch
> back and forth depending on your need for synchronized blocks (which is
> really the only reason you'd switch). Collections.synchronizedList may
> look more complex, but not when you consider that you use almost
> identical code to synchronize a Map or Set as well.


Also, the only thing that needs to change is the creation of the object.
That is, assuming you follow the expectations of the collections framework
and use List as the object type.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
Shane Mingins

2004-07-28, 9:09 pm

"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.1b7096c066cf8d11989751@news.altopia.net...
>
> 2. It's more consistent to use ArrayList all the time rather than switch
> back and forth depending on your need for synchronized blocks (which is
> really the only reason you'd switch). Collections.synchronizedList may
> look more complex, but not when you consider that you use almost
> identical code to synchronize a Map or Set as well.
>
> --
> www.designacourse.com
> The Easiest Way to Train Anyone... Anywhere.
>
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation



Shane Mingins

2004-07-28, 9:09 pm

"Chris Smith" <cdsmith@twu.net> wrote in message
news:MPG.1b7096c066cf8d11989751@news.altopia.net...
> Second, yes that's basically it; ArrayList and Vector are functionally
> identical. Their performance is not even likely to be all that
> different. There are a couple reasons to choose ArrayList over Vector,
> though:


Don't u hate it when u accidentally hit send before you've typed your
message :-( Ignore my other non-reply!

JList supports a Vector as it's list data ... so I find I am creating/using
Vectors for that ... just wondered if I may have been missing something
there?

Cheers
Shane


--
"Dear Lord: The gods have been good to me. For the first time in my life,
everything is absolutely perfect just the way it is. So here's the deal: You
freeze everything the way it is, and I won't ask for anything more. If that
is OK, please give me absolutely no sign. OK, deal. In gratitude, I present
you this offering of cookies and milk. If you want me to eat them for you,
give me no sign. Thy will be done." - Homer Simpson


Tony Morris

2004-07-30, 8:56 pm

When you must support a pre-1.2 VM.
The same can be said for java.util.Hashtable

--
Tony Morris
http://xdweb.net/~dibblego/



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com