One should prefer to use ArrayList over Vector because we rarely need what vector has extra to offer.
What is difference between Vector and ArrayList, which one shall be preferred
Carvia Tech | December 25, 2017 | 1 min read | 238 views
Vector is synchronized for concurrent modification. But Vector synchronizes on each individual operation. That’s almost never what one want to do. Generally you want to synchronize a whole sequence of operations.
Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?
There is always a overhead (CPU and Memory) of locking even when you don’t need to.
In almost all scenario’s you can utilize ArrayList in your application code, if you are looking for a synchronized version of List you can decorate a collection using the calls such as Collections.synchronizedList(List<T> list), as shown below-
List<String> persons = new ArrayList<>(); List<String> synchronizedList = Collections.synchronizedList(persons);
As for a Stack equivalent - you can have a look at Deque/ArrayDeque.
As a result, Vector is often considered deprecated nowadays.
Summary of Differences between Vector and ArrayList
Vector methods is synchronized, ArrayList is not.
Data Growth - Vector doubles its size when its full, ArrayList increases its size by 50% when its full.
Vector class retrofitted to implement the List interface, making it a member of the Java Collections Framework in JDK 1.2
Top articles in this category:
- What is difference between ExecutorService submit and execute method
- What is difference between HashMap and HashSet
- Difference between Callable and Runnable Interface
- What is difference between sleep() and wait() method in Java?
- Difference between HashMap and ConcurrentHashMap
- Difference between HashMap, LinkedHashMap and TreeMap
- What is purpose of Collections.unmodifiableCollection