• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

ArrayList vs. Vector

 
Ranch Hand
Posts: 249
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help!!
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    No more fooling around. Read this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic