my dog learned polymorphism*
The moose likes Performance and the fly likes Vector V/s ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector V/s ArrayList" Watch "Vector V/s ArrayList" New topic
Author

Vector V/s ArrayList

Viral Thakkar
Ranch Hand

Joined: Jan 30, 2001
Posts: 33
Hi All,

I am using Toplink. Methods of Toplink return Vector. I require to iterator on this Vector.

Should I create a ArrayList from this Vector and then iterate on it?

What is advicable for performance?

Regards,
Viral
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
Vector is synchronized


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Viral Thakkar
Ranch Hand

Joined: Jan 30, 2001
Posts: 33
So before iterating, should I create a ArrayList from the Vector and then iterate over ArrayList?
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
As I understand it (and as Billy hints), the only performance difference between Vector and ArrayList is that the former is synchronised. So, it can be slower in multithreaded apps, but you might need synchronisation. (Though in that case if you had an ArrayList you could use Collections.synchronizedList(myArrayList) to synchronise it).

In your situation the other concern is the cost of converting a Vector to an ArrayList. Whether or not this is a problem depends on the size of the vector and performance requirements of your app.

The other concern here is which is a better class to use? The general consensus is that (all things being equal) you should use ArrayList, as it's part of the newer Collections framework. Vector is a 'legacy class'.

In your situation, I would stick with the vector if you don't need to pass the object around, or convert it to an ArrayList as soon as you get it, so you don't use the legacy Vector class through your app.

Still, each situation is different



--Tim
[ June 28, 2004: Message edited by: Tim West ]
sander hautvast
Ranch Hand

Joined: Oct 18, 2002
Posts: 71
i object to the term 'legacy class'. both Vector and ArrayList internally use an array to store data and both use the same mechanism for growing (native copying of the array to a bigger array). Vector is a Collection just as ArrayList. So the only difference really is the synchronisation. I do agree that every situation requires close look as to what class to use.)
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Hmm, looks like I spoke too boldly (read: incorrectly) on this.

I didn't realise that as per the Vector JavaDoc, Vector has been retrofitted into the Collections API - it now implements List.

So, I guess you can ignore that part of my post above. Thanks Sander

Do you object to the term 'legacy class' in general, or just in this instance? It's certainly a term that's in use in the JDK JavaDoc, see here for example.


--Tim
Kris Philippaerts
Greenhorn

Joined: Oct 19, 2001
Posts: 25
Originally posted by Tim West:
So, it can be slower in multithreaded apps, but you might need synchronisation.


Synchronized code will always have an influence on the performance of your code. Even in non-multithreaded apps, the overhead can be significant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector V/s ArrayList