go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Java Collections » ArrayLists vs. Vectors
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: ArrayLists vs. Vectors
Alex_Raj
member
offline   
 
posts: 99
joined: 05/16/2006
from: San Jose, CA
  posted on: 01/18/2007 02:45:55 PM    Edit  |   Quote  |   Report 
ArrayLists vs. Vectors
When handling data sets, people prefer arrays as long as the data sets are homogeneous collection of elements, as arrays are fast to execute, have implicit type check and easy to handle. But when there are situations here you need to handle the data that grows during run time. The obvious choices available with java are ArrayList or Vector. Most of the time, people use both these data structures interchangeably though but the question is also obvious: which one is better, ArrayList or Vector?

Similarities between ArrayList and Vector

  • Both can grow up during run time.
  • Both implement List interface.
  • With both, it's efficient to remove or add elements at the end or the beginning, but it's expensive if you try to add or remove elements somewhere in middle of collection. Use LinkedLists instead for those situations.

    Differences between ArrayList and Vector

  • The major difference, as the documentation says, is just that vectors are synchronized. If more than one thread in your code is to use that data, you are in trouble with ArrayList as the data is not synchronized. Though there are ways by which you can make your ArrayLists synchronous, but by default they are not. The obvious downside with vectors is the additional computation to handle threads.
  • The other difference is that with vectors, you can specify the incremental value, which is the amount with which the data structure will grow during the run time. But with ArrayLists you have no option but to accept default that is the list will grow up 50% of original size every time it needs additional space. It is advisable in both the cases to choose the initial size carefully.

    Conclusions:
  • Use Arrays as much as possible if your collections is size predictable, as they are fast and simple;
  • In situations where data set is completely dynamic, use ArrayList at the first choice if your code is threadsafe, otherwise Vectors.

  •  Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.