• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector V/s ArrayList

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vector is synchronized
 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So before iterating, should I create a ArrayList from the Vector and then iterate over ArrayList?
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic