This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector V/s ArrayList

 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector is synchronized
 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So before iterating, should I create a ArrayList from the Vector and then iterate over ArrayList?
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic