Thursday 2 May 2013

Difference between Vector and ArrayList in java?


  • All the methods of Vector is synchronized. 
  • But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.
  • Vector and ArrayList both uses Array internally as data structure. 
  • They are dynamically resizable. 
  • Both ArrayList and Vector maintains the insertion order of element.
  • Difference is in the way they are internally resized. 
  • By default, Vector doubles the size of its array when its size is increased. 
  • ArrayList increases by half of its size when its size is increased.
  • Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.
  • In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. 
  • But, when there is a certain need for thread-safe operation Vector needs to be used.


No comments:

Post a Comment