• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList vs. Vector  RSS feed

 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a post in the JSP forum, a suggestion was made to use an ArrayList instead of a Vector for perforance reasons.
So here is my question:
When is an ArrayList preferable to a Vector and vice versa?
Thanks in advance.
 
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
They're quite similar except that Vector is synchronized and ArrayList is not (the algorithm they use to reallocate space is also slightly different, but generally this isn't as much of a deciding factor.) Since you can use Collections.synchronizedList() to make a synchronized version of ArrayList when necessary, my answer is basically it's never appropriate to use Vector in new code. The only reason to use it is if you have to interface to older code that has Vector method parameters or return values.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between the 2 is that Vector is automatically synchronized. Since that behavior is not always desired (most cases), they created ArrayList (I beleieve in JDK 1.2) to replace Vector. You also gain flexibility since ArrayList implements the List interface.
you should always use ArrayList. If you need it to be synchronized, then use Collections.synchronizedList(myArrayList), which creates a synchronized wrapper for your ArrayList.
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help!!
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
It depends on your program and its requirements.
The main things to consider are the following:
Thread safety: Vectors are synchronized and hence threadsafe. ArrayLists on the other hand, are not. So if thread safety is an issue, go for Vectors. If not, ArrayLists will give better performance. (And yes, as mentioned in posts before, Vectors are not for new code anymore)
Growth of Data: Both ArrayLists and Vectors use Arrays internally. When an element is inserted into a Vector, beyond its initial capacity, a Vector doubles the size of its array. The ArrayList, on the other hand increases its internal array size by 50 percent. This is obviously a big performance hit while adding new elements. It's recommended to set the object's initial capacity to the largest capacity that you think your collection will ever need.
You can visit this link from Javaworld, to get a better view of this discussion: http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html . But I think the first point is the one that answers your question: If threadsafety is not an issue, go for ArrayLists.
I hope that helps.
Cheers!
[ March 10, 2004: Message edited by: Aneesha Singh ]
[ March 10, 2004: Message edited by: Aneesha Singh ]
 
santhoshkumar kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList and Vector uses Array as its basic data structure. There are two basic difference between them.
1. Resizing when it gets full - ArrayList increases itself to 50% of it whereas Vector doubles itself - So if no continues increase to size ArrayList is good.
2. Synchronization - All the methods and individual operations are synchronized in case of vector (It may not required most of the cases).

Synchronization results in performance degradation. This is the reason to prefer ArrayList than Vector. If you require more on this. See below posts:
blog post - Ths gives the differences and similarities
stackoverflow - It says the vector is deprecated
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhoshkumar kumar wrote:stackoverflow - It says the vector is deprecated

Class Vector is not officially deprecated (it does not have a @Deprecated annotation or Javadoc 'deprecated' tag) - if it were, you would get warnings from the compiler when you would try to use it.

There is, however, no good reason to use Vector in new code - Vector is a lecagy collection class from Java 1.0 and 1.1. In Java 1.2 (a very long time ago - I think this came out in 1999) the collections framework was added, with ArrayList as the replacement for Vector.
 
santhoshkumar kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList and Vector uses Array as its basic data structure. There are two basic difference between them.
1. Resizing when it gets full - ArrayList increases itself to 50% of it whereas Vector doubles itself - So if no continues increase to size ArrayList is good.
2. Synchronization - All the methods and individual operations are synchronized in case of vector (It may not required most of the cases).

Synchronization results in performance degradation. This is the reason to prefer ArrayList than Vector. If you require more on this. See below posts:
blog post - Ths gives the differences and similarities
stackoverflow - It says the vector is deprecated (Considered to be deprecated)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhoshkumar kumar wrote:ArrayList and Vector...

Is there some reason you've repeated a post to a ten-year old thread?

Winston
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mistaken at least thrice:
  • In your blog you go on about variable size arrays. Neither Vector nor ArrayList is an array. They are Lists.
  • The bit about better performance when you don't resize a collection is simply meaningless.
  • The SO post you link does not say that Vector is deprecated.
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!