ArrayList<T> al = new ArrayList<T>(); Vector<T> v = new Vector<T>();
Major Differences between ArrayList and Vector:
- Synchronization : Vector is synchronized, which means only one thread at a time can access the code, while arrayList is not synchronized, which means multiple threads can work on arrayList at the same time. For example, if one thread is performing an add operation, then there can be another thread performing a remove operation in a multithreading environment.
If multiple threads access arrayList concurrently, then we must synchronize the block of the code which modifies the list structurally, or alternatively allow simple element modifications. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
- Performance: ArrayList is faster, since it is non-synchronized, while vector operations give slower performance since they are synchronized (thread-safe). If one thread works on a vector, it has acquired a lock on it, which forces any other thread wanting to work on it to have to wait until the lock is released.
- Data Growth: ArrayList and Vector both grow and shrink dynamically to maintain optimal use of storage – but the way they resize is different. ArrayList increments 50% of the current array size if the number of elements exceeds its capacity, while vector increments 100% – essentially doubling the current array size.
- Traversal: Vector can use both Enumeration and Iterator for traversing over elements of vector while ArrayList can only use Iterator for traversing.
- Applications : Most of the time, programmers prefer ArrayList over Vector because ArrayList can be synchronized explicitly using Collections.synchronizedList.
Note: ArrayList is preferable when there is no specific requirement to use vector.
ArrayList elements are: Practice.GeeksforGeeks.org quiz.GeeksforGeeks.org code.GeeksforGeeks.org contribute.GeeksforGeeks.org Vector elements are: Practice quiz code
How to choose between ArrayList and Vector?
- ArrayList is unsynchronized and not thread-safe, whereas Vectors are. Only one thread can call methods on a Vector at a time, which is a slight overhead, but helpful when safety is a concern. Therefore, in a single-threaded case, arrayList is the obvious choice, but where multithreading is concerned, vectors are often preferable.
- If we don’t know how much data we are going to have, but know the rate at which it grows, Vector has an advantage, since we can set the increment value in vectors.
- ArrayList is newer and faster. If we don’t have any explicit requirements for using either of them, we use ArrayList over vector.
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- ArrayList of ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- Array vs ArrayList in Java
- ArrayList in Java
- ArrayList vs LinkedList in Java
- How to remove an element from ArrayList in Java?
- ArrayList to Array Conversion in Java : toArray() Methods
- ArrayList and LinkedList remove() methods in Java with Examples
- Conversion of Array To ArrayList in Java
- Synchronization of ArrayList in Java
- Custom ArrayList in Java
- Initialize an ArrayList in Java
- ArrayList toArray() method in Java with Examples
- Arraylist removeRange() in Java with examples
- ArrayList get(index) method in Java with examples
- Arraylist lastIndexOf() in Java with example
- Arraylist.contains() in Java
- ArrayList trimToSize() in Java with example
Improved By : Koolstr