Difference between Vector and ArrayList in Java?

  • Vector is a legacy class that came along with the first version of java development kit (JDK), while ArrayList was introduced in java version1.2, as part of java collections framework. 
  • As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement List and vector also became a part of java collection framework.
  • Vector is synchronized (Thread safe). But, 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 re-sizable. Difference is in the way they are internally re-sized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
  • Major difference between Vector and ArrayList is that of synchronization. Vector’s methods are synchronized and ArrayList’s methods are not synchronized.


 Which is better to use? Vector or ArrayList?

In general, executing a ‘synchronized’ method results in costly 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.

What is the alternative of Vector when it comes to thread safety? 

  • ArrayList is the best alternative to Vector as it can be synchronized using the java collections framework utility class. 
  • When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.
  • When you use Vector or ArrayList, always initialize to the correct capacity of collection as auto-incrementing the size of collection is a costly operation.

No comments:

Post a Comment