IT Community - Software Programming, Web Development and Technical Support

Which is best Vector or ArrayList?

This is a discussion on Which is best Vector or ArrayList? within the Java Programming forums, part of the Software Development category; Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-24-2007, 11:22 PM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Question Which is best Vector or ArrayList?

Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-24-2007, 11:26 PM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default Re: Which is best Vector or ArrayList?

The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-24-2007, 11:38 PM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Which is best Vector or ArrayList?

I have seen many posts warning people to staty away from Vector and use ArrayList since ArrayList is "much faster".

I have done time tests (using 1.3) and found no real difference. On occasion, Vector was even faster. If you look at its implementation, Vector is backed by an array which makes access quick.

Since Vector is what is also used by older existing classes and it is also a List, I think that Vector is a better choice.

Does anyone have any specific evidence that ArrayList is so much better than Vector?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-24-2007, 11:51 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: Which is best Vector or ArrayList?

Hi,

Vector has another benefit too -

it is a thread save class - you can add something to the vector or remove something and must not care about other threads, that have access to the vector at the same time. A Vector is easy to use and, I guess, it is used by DefaultTableModel (model from JTable), because it is thread save - you can access the model by one thread without keeping track of concurrently access by the event handling thread for example.

So if you have the choice, choose Vector - if the difference in execution time is relevant for your application, you should better look out for a better algorithm, because the difference is only marginal and you will not win much time by using ArrayList instead of Vector.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-24-2007, 11:53 PM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: Which is best Vector or ArrayList?

The difference between the two is that a Vector is synchronized and an ArrayList is not. Since the Vector is synchronized it has overhead for each call to the Vector.

The Vector may appear to be eaqually as fast in a simple test, but it is in fact slower.
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-25-2007, 12:01 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Which is best Vector or ArrayList?

Since my time tests couldn't find any difference, does anyone have specific results of tests which show how much slower Vector actually is than ArrayList?

It would be good if I knew what the difference was so I knew how much weight to give the time in design considerations. Is the difference just fractions of a percent? . I was testing enough iterations that I think Iwould have found differences if they were several percent.

While I do believe that being synchronized has some overhead, it may be a trivial amount. Some people seem emphatic that Vector is bad and ArrayList is good, but without any specifics.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-25-2007, 12:04 AM
saloni srivatsava saloni srivatsava is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 16
saloni srivatsava is on a distinguished road
Default Re: Which is best Vector or ArrayList?

BruceEckel.com has some performance numbers I believe regarding synchronization. I think I read that synchronized calls can take up to 4 times as long.

Don't forget also the synchronization affects Enumeration vs Iterator and other helper classes to the Vector.

While ArrayList can be faster, it is probably not enough in most applications. We ran some performance figures once and we needed hundreds of thousands of entries before the difference became appreciable. I always use ArrayList but it's just preference the synchronization isn't always good. ArrayList also had many other new methods that are from the Map family which are nice to have.

But if you like Vectors use them. You probably heard the first rule of performance tuning is "don't do it"; i.e. don't spend time trying to make something faster until you know where your bottlenecks are. Esp. don't rewrite a bunch of old code to use Vectors it will not be worth it.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-25-2007, 12:05 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Which is best Vector or ArrayList?

The perfemance time for synchronization seems like just what I need. Where in the Bruce Eckel site would I need to go to find that information. Is it free ot do I need to buy one of their books/seminars?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Cannot Find Symbol" when I trying Vector to ArrayList amansundar Java Programming 2 11-28-2007 02:18 AM
Type conversion from arraylist to string in C# oxygen C# Programming 3 07-30-2007 09:47 AM
Difference between Array and ArrayList leoraja8 Java Programming 1 05-11-2007 04:06 AM
Arraylist leoraja8 Java Programming 1 05-11-2007 03:47 AM
Convert Object to Arraylist and Arraylist to Object raja C# Programming 0 05-08-2007 03:26 AM


All times are GMT -7. The time now is 11:06 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0