• Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any diff in arraylist n vector other then synchronization  RSS feed

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybdy tell me any Difference between Vector and ArrayList other than Synchronization?

thanx
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both ArrayList and Vector implement the List interface, but Vector has a bunch of other methods that duplicate the functionality of those List methods, but have uglier names: for example, addElement(). Vector also provides a methat that returns an old-fashioned Enumeration are well as an Iterator.

The one additional difference is that the algorithms used to grow the underlying array when elements are added is a little different. Vector's will use, on average, a lot more unused space, but may be a little but faster under certain circumstances.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

Vector list by default is of 10 size where Array list is of 0 size


hope this is of use to you

cheers
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by faisal usmani:

Vector list by default is of 10 size where Array list is of 0 size


Where did you get that idea? From ArrayList.java:
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I recall according to the Sun documentation on Collections you should use ArrayList in favor of Vector. It's meant as a replacement but Vector will live on at least for the forseeable future in order to meet a binary compatibility requirement. I say "as I recall" because I'm 99% sure of this but I haven't gone back and looked to ensure my memory is accurate, so take it for what it's worth.

When is Vector faster Ernest?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:

When is Vector faster Ernest?


Let's say you have an array of size 1, and you're going to add some indeterminate number of elements to it, one after another. Whenever the array is full, you need to allocate a new larger one and copy the old elements into it. This copying is computationally expensive, so you want to do it as infrequently as possible. On the other hand, the only way to reduce the number of allocate/copy cycles is to make bigger allocations, which uses more space: so it's a classic space/speed tradeoff.

Vector doubles the size of the array each time it does a reallocation. Therefore on the average, it uses 25% more memory than it needs. ArrayList, on the other hand, grows the array by a fixed chunk size. Therefore, the amount of extra storage is fixed, and generally much smaller for large Lists.

But note that Vector only has to allocate/copy ln(N) times to populate a collection of size N, whereas ArrayList has to do it N/(chunksize) times. Therefore, if adding a very large but unknown number of elements to a collection can be the performance-limiting step in an application, Vector could, theoretically, perform better. In practice, this advantage may never really be observed.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it's only going to be noticed if you're adding a large number of elements. Even then only if the developer does not or cannot use ensureCapacity() to grow the ArrayList once before it adds a big chunk of elements. I would humbly suggest that while correct it might be better to not mention it as it might mislead the already confused into thinking Vector will provide them better performance at the cost of memory.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
I would humbly suggest that while correct it might be better to not mention it as it might mislead the already confused into thinking Vector will provide them better performance at the cost of memory.


I agree about confusing people, but I'm damned if I do, and damned if I don't. If I didn't mention it, Jim would come along behind me and point out that I forgot this essential difference between the two classes.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
When is Vector faster Ernest?


Both Vector and ArrayList have their own "reallocate, copy, store" (better name?) algorithm that is hard-coded into the type. In theory, one could create a scenario that takes advantage of the Vector algorithm such that a performance improvement could be observed. Wouldn't it be nice if users could provide the algorithm strategy for themselves? ...since such an algorithm is not intrinsic to the requirement of "a sequentially ordered list of elements", which is the single role that ArrayList/Vector attempt to achieve (but as per usual J2SE API Specification, over-achieve).

Here's a little bit more:
http://contractualj.com/faq/#faq3
Why isn't there a mutable sequence implementation that is backed by an array?
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the synchronization overheads big enough nowadays in JDK 1.4 or JDK 5?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!