In this article, we will discuss difference between ArrayList and Vector classes in detail i.e; ArrayList v/s Vector
1. ArrayList v/s Vector:
ArrayList | Vector |
ArrayList is introduced in the original collection framework in Java 1.2 version | Vector is a legacy class including Stack, Dictionary, HashTable & Properties and introduced in Java 1.0 version |
ArrayList methods are non-synchronized | All legacy collection classes are synchronized, thus Vector is synchronized (i.e.; all methods of Vector class is synchronized) |
As ArrayList is non-synchronized, hence it isn’t thread-safe. So, programmer need to handle thread-safety while working in multi-threaded environment | As Vectror is synchronized, hence it is thread-safe. So, no need to worry while working in multi-threaded environment, as only one thread get chance to work at any given time |
This is comparatively faster as it is non-synchronized, as threads doesn’t require to obtain lock before operating on ArrayList | Performance-wise vector is slower comparing with ArrayList due to synchronization, as threads need to wait for their chance to operate on Vector object |
ArrayList increases its size by 50% of current array, when its capacity exceeds | Vector increases its size by 100% of current array, when its capacity exceeds |
Only Iterator is allowed to iterate item/elements inside ArrayList | Both Iterator & Enumeration can be used to iterate item/elements inside Vector |
ArrayList can be converted into synchronized ArrayList using static utility methods of Collections class
Collection.synchronizedList(arrayList); | No need to do that, as already Vector is synchronized by default |
Q) When to use ArrayList ?
- If performance is the factor while storing element/objects, then ArrayList is apt
- But definitely extra precautions need to be taken while working with multil-threaded environment
- Also, check how much extra space is required when List is full; if 50% of original size if required then ArrayList will fits the case perfectly
Q) When to use Vector ?
- If we aren’t concerned with performance, but element/objects need to be accessed in thread-safe manner, then Vector is good choice
- But performance will be a big hit, as every thread to need to wait to obtain lock before accessing vector element/objects
- Here, size increase in 2 times the original size; so if there are more number of items to be added then Vector will fits the bill perfectly
Related Articles:
- List interface
- ArrayList class
- LinkedList class
- Vector class
- Stack class
- ArrayList v/s Vector
- ArrayList v/s LinkedList
- List v/s Set
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html
Happy Coding !!
Happy Learning !!