Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList vs Vector

 
Harsha Hegde
Greenhorn
Posts: 17
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is vector a performance friendly data structure? Performance point of view how different is it from ArrayList?
Regards,
Harsha
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5387
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charl wrote:Hi All,

That's Perfect...


 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Not true, and please stop making stuff up. First, it's not random


Yes it is Runtime.
 
shishir guptaa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10108
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and the idea of using Vector for "thread safely" is dangerously, horribly misleading. Even if it is what Sun said.
 
shishir guptaa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic