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

ArrayList vs Vector

Harsha Hegde
Greenhorn

Joined: Jan 12, 2009
Posts: 17

Hi,
Is vector a performance friendly data structure? Performance point of view how different is it from ArrayList?
Regards,
Harsha
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Vector is slower because it causes people to waste time asking about it when they should just be using ArrayList. Just forget about Vector, and your life will be easier. You will be able to spend time on something that matters, instead.
Pankaj Kr. Singh
Ranch Hand

Joined: Mar 31, 2011
Posts: 32

Hi Harsha Hegde,

Vector is slower than the Arraylist because all the methods of Vector are synchronized but the Arraylist methods and not synchronized so it is faster than Vector.
If you want your application to be thread safe then you can use Vector instead of using Arraylist.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

If performance really matters, the real question is ArrayList or LinkedList? And the answer to that depends on how you're using it (ArrayLists being faster for random access reading, LinkedLists generally being faster for modification).
venkat prathap
Greenhorn

Joined: Jun 06, 2011
Posts: 7

while selecting Vector or Arraylist, it depends on 2 factors...whether our collection should be threadsafe/fast access ie performance.

if we go with threadsafe, it is better to go with Vector, Vector is synchronized ie all methods in the vector are synchronized.so when synchronized only one thread can access the list.

In case of Arraylist, this is not threadsafe ie not synchronized...so when it is not synchronized there is no limit in case of threads also.


In general..if we just accessing(read) the list , it would be suggestable to go with arraylist.in case of add/update of list its better to go with vector, it provides thread safe..


Thanks,
Venkat.
Pene charl
Greenhorn

Joined: Jun 14, 2011
Posts: 21


Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.

Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.


Regards:Prince Charles
http://www.princecharl.blogspot.com
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

venkat prathap wrote:In general..if we just accessing(read) the list , it would be suggestable to go with arraylist.in case of add/update of list its better to go with vector, it provides thread safe..


In a large number of cases where thread interactions are important, it's not enough to just synchronize the individual methods. You'll often, for instance, need to synchronize an entire block iterating over the list. So using a Vector you get the performance hit, and the illusion of thread safety.

Better to use ArrayList everywhere, and add your own synchronization where it is needed.
Pankaj Kr. Singh
Ranch Hand

Joined: Mar 31, 2011
Posts: 32

to use vector and Arraylist depends on the requirements you have.If you need thread safe use vector. if you require performance use arraylist
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

No no no, never ever use Vector. If synchronization is important, use the synchronizedList() method in the java.util.Collections class instead.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Hrrmm. If synchronization is important, you probably shouldn't put too much faith in either Collections.synchonizedList() or Vector. You probably need additional external synchronization to use them in a thread-safe manner (e.g. when iterating, but also in many other common cases). Unless the list is intended to be effectively immutable (or at least unchanged) after initialization. In that case, nowadays it's better to use an ImmutableList.

Anyway, death to Vector! Along with most of the other mutable "thread-safe" classes Sun gave us.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Hi All,

As the size of vector increase at random time so it is efficent only at the time when you donot know how many object you need to store in it..
So you are talking about performance then it is fact that there is a difference between ArrayList and Vector,and its absolutely true that Vector is little bit slower as compare to ArrayList,but the fact is difference is not that much as we expect.

if we use vector properly then it is good enough.

Vector vec=Vector(int initialCapacity, int capacityIncrement) ;

So take here initial capacity as whatever you expect,and take capacityIncrement as low as possible may be 2 or 3.

Vector vec=Vector(5,2) ;

Now this will not waste much memory at runtime if you Vector size will increase at runtime.

Thanks
Shanky




SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Mostly what i have seen in most of the code is people use ArrayList and they donot know how much object they need to store in it.
So they mark its initial capacity as
ArrayList arr=new ArrayList(2500);


Here if at runtime you arraylist size grows more then 2499 then it will throw an ArrayIndexoutOfBoundException and if it desnot reach its maximum limit and stop at 2300 then unnessarily you are wasting memory here..which is not the case with Vector..

So its totally depends upon your requirement what you want to use
Pene charl
Greenhorn

Joined: Jun 14, 2011
Posts: 21

Charl wrote:Hi All,

That's Perfect...


Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Shanky Sohar wrote:Here if at runtime you arraylist size grows more then 2499 then it will throw an ArrayIndexoutOfBoundException

Not even remotely true - that's the whole point of using Vector or ArrayList: they grow as necessary.

Shanky Sohar wrote:and if it desnot reach its maximum limit and stop at 2300 then unnessarily you are wasting memory here..which is not the case with Vector..

Also not true - Vector would waste exactly the same amount of memory in this case, no more, no less.
Sudheer Bhat
Ranch Hand

Joined: Feb 22, 2011
Posts: 75
Shanky Sohar wrote:
Vector vec=Vector(int initialCapacity, int capacityIncrement) ;

So take here initial capacity as whatever you expect,and take capacityIncrement as low as possible may be 2 or 3.


I don't think keeping the capacityIncrement to a very low value when the Vector can grow is not a good idea either (if at all one still needs to make use of vector!). Very low values of capacityIncrement would result in frequent call to Arrays.copyOf which in turn calls System.arrayCopy. Assuming a value of 2 for capacityIncrement, every 3rd addition to the Vector after initial capacity would result in a call to Arrays.copyOf method.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Oh, wait, there was an earlier post from Shanky? Sorry, I missed that. Let me correct my oversight:

Shanky Sohar wrote:As the size of vector increase at random time

Not true, and please stop making stuff up. First, it's not random, and second, this is not a significant difference between Vector and ArrayList. Both Vector and ArrayList increase their sizes on very predictable, non-random schedules. Those schedules are different, true: Vector doubles in size when it needs to grow, while ArrayList increases size by 50%, plus one. But both are equally predictable and non-random.

Shanky Sohar wrote:if we use vector properly then it is good enough.

Vector vec=Vector(int initialCapacity, int capacityIncrement) ;

Frankly, any attempt to set the capacityIncrement on Vector is a horrible, horrible idea, from a performance perspective. In fact the person who added this option to Vector in the first place should be publicly shamed. It's a stupid idea, which leads to poorer and poorer performance as more items are added to the Vector. Fortunately this idea was dropped from ArrayList, which is yet another example of why Vector should be dropped and forgotten.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Mike Simmons wrote:Not true, and please stop making stuff up. First, it's not random


Yes it is Runtime.
shishir guptaa
Greenhorn

Joined: Dec 23, 2010
Posts: 3
Vector is synchronized and Arraylist is not. By, synchronized it means here is, any method that touches Vector's content, is sync (or read it as thread safte).
Practically, for normal execution where you expect program to run in a single threaded env, use ArrayList and when in doubt or to protect data at any cost,
use synchronizedList from Collections api. Below is the link.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#synchronizedList(java.util.List)

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7773
    
  21

shishir guptaa wrote:Vector is synchronized and Arraylist is not...

Do you realize that you replied to a thread that is 18 months old? I suspect Elvis (or rather Harsha) has left the building.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Also, nothing new has been added that wasn't already talked about. And your API link is ancient. Are you really still using Java 1.4?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Oh, and the idea of using Vector for "thread safely" is dangerously, horribly misleading. Even if it is what Sun said.
shishir guptaa
Greenhorn

Joined: Dec 23, 2010
Posts: 3
Winston Gutkowski wrote:
Do you realize that you replied to a thread that is 18 months old? I suspect Elvis (or rather Harsha) has left the building.

Winston


i actually missed the date...will post carefully in future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList vs Vector