This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes  What is the use of Vector where as Arraylist can be Synchronized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark " What is the use of Vector where as Arraylist can be Synchronized?" Watch " What is the use of Vector where as Arraylist can be Synchronized?" New topic
Author

What is the use of Vector where as Arraylist can be Synchronized?

Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

Hi All,
It may silly question but any how need to know more details.

The difference between Vector and ArrayList is thread safe whereas Arraylist is not. but as the arraylist can be make synchronized what is the necessity of Vector?

also Enumeration and Iterator?

thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Exactly. Don’t use Vector, Enumeration, Stack or Hashtable.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Vector existed before there was any such thing as ArrayList. in the beginning it was either Vector or Array, that was it!
as for iterators, i never liked them anyway. i am so glad we now have "for each" so we no longer need them


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Randall Twede wrote: . . . i am so glad we now have "for each" so we no longer need them [Iterators]
But the for‑each loop always uses an Iterator. It is simply at one remove where you don’t notice it.
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

No, ArrayList cannot truly be synchronized without more effort than simply using a Vector.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I still strongly prefer ArrayList over Vector.

Firstly, Collections.synchronizedList(new ArrayList()); does not look like an unreasonable amount of effort to me.

Secondly, when someone spots this construction, it is immediately clear the ArrayList is intentionally synchronized and is therefore going to be used by multiple threads. If I used a Vector instead, the developer to come after me might not know for sure whether I had a bout of nostalgia, or I really use Vector just to get synchronized access.

For me, Vector equals legacy code.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

nah! you are kidding me. how can such a succinct, non-verbose, language like Java have legacy code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
It was written as legacy code when it was new
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

Martin Vajsar wrote:I still strongly prefer ArrayList over Vector.

Firstly, Collections.synchronizedList(new ArrayList()); does not look like an unreasonable amount of effort to me.

Secondly, when someone spots this construction, it is immediately clear the ArrayList is intentionally synchronized and is therefore going to be used by multiple threads. If I used a Vector instead, the developer to come after me might not know for sure whether I had a bout of nostalgia, or I really use Vector just to get synchronized access.

For me, Vector equals legacy code.


Yeah, I stand corrected. Vectors are pre 1.2 and shouldn't really be used with the newer collections in the Java API.

I wonder why it's not deprecated?

Here is a quote from Jon Skeet explaining (http://stackoverflow.com/a/1386288/603732):

Jon Skeet wrote:
Vector synchronizes on each individual operation. That's almost never what you want to do.

Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time) but also slower (why take out a lock repeatedly when once will be enough)?

Of course, it also has the overhead of locking even when you don't need to.

Basically, it's a very flawed approach to synchronization in most situations. As MrSpandex pointed out, you can decorate a collection using the calls such as Collections.synchronizedList - the fact that Vector combines both the "resized array" collection implementation with the "synchronize every operation" bit is another example of poor design; the decoration approach gives cleaner separation of concerns.

As for a Stack equivalent - I'd look at Deque/ArrayDeque to start with.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rishi Shah wrote:I wonder why it's not deprecated?


It isn't deprecated because there's an awful lot of existing code in the standard API which uses it. See DefaultTableModel for an example. Since the published API of that class involves Vector parameters, it can't ever be changed. Of course, DefaultTableModel itself could be deprecated in favour of a new design which used List instead of Vector, but that would be a lot of work for really not much value.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38389
    
  23
Can’t they overload the method to take a List parameter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of Vector where as Arraylist can be Synchronized?