go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  ArrayLists vs. Vectors
 
Subject: ArrayLists vs. Vectors
Author: Alex_Raj
Posted on: 01/18/2007 02:45:55 PM

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.




    References:

  •  


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