File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Vector performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector performance" Watch "Vector performance" New topic

Vector performance

Ashish Bassi

Joined: Aug 22, 2007
Posts: 11
I have a method which needs to return a Vector object. Now, I realize that I dont really need it to be a Vector here and an ArrayList would do the job too since i dont need a synchronized collection. However, due to framework limitations, the return type must be a vector.

I was wondering if I could improve performance if I add objects to an ArrayList, and convert it into a Vector by doing something like

List list = new ArrayList();

return new Vector(list);

Or would simply adding objects to a Vector be better than doing the above?

Can i create a non-synchronized subclass of Vector which I can return in my method?

Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Adding the Objects directly into Vector instance is enough...

Former approach leads to creation of ArrayList instance redundantly...

SCJP 6 [SCJP - Old is Gold]
Sudheer Bhat
Ranch Hand

Joined: Feb 22, 2011
Posts: 75
From the source of

Note:I haven't done any bench marking on whether creating ArrayList and then returning Vector(list) will have any notable performance improvement or not. So I am not really sure which one would be better. I feel returning Vector(list) may be good if the number of elements that needs to be added to the vector is high.
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

The (main) performance issue with the vector class (if initialized with the proper size) is the synchronization required while adding elements. However, a modern JVM (up to date 1.6+) will inspect your code and identify that you're just accessing a local instance without any chance of another thread interfering. Therefore, it will remove the synchronization overhead at runtime, so can probably just ignore the performance considerations in this case and directly add to the Vector instance.
I agree. Here's the link:
subject: Vector performance
It's not a secret anymore!