What are the differences between ArrayList and Vector?
- Vectors are synchronized, ArrayLists
- Data Growth Methods
Use ArrayLists if there is no specific requirement to use Vectors.
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
Collections.synchronizedList is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.
Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
ArrayList vs Vector : Which one to use?
Other than differences with synchronization, there is also a difference internally when you insert new elements. For both classes, the array within must be increased in size to prevent it from running out of room. Vectors double their size by default. ArrayLists increase their size by half their present size.
Also, a couple answers mentioned that Vectors are thread safe. That's true to some extent, but the whole reason they're sort-of-deprecated (I think) is because they're not very useful for most synchronization needs, which is because it synchronizes on every operation (not safe). What you want to do usually is synchronize on a sequence of operations. Vectors don't do that (neither do ArrayLists though) so it's not really the way to go even in situations that require synchronization (basically it's out of your control).
ArrayList vs. Vectors in Java if thread safety isn't a concern
Vector originates back from the pre-Collections API days, and have been retrofitted since to be a part of it. From what I've read, the reason it is not deprecated is because the core API depends on it.
ArrayList was written from scratch as a part of the Collections API and as such should be used unless you need to support Java versions down to 1.2.
If you need a thread-safe ArrayList, you can use the static factory method
Collections.synchronizedList(new ArrayList<type>); to generate your list.
Collections: Array, Vector and ArrayList. Differences and appropriate usage
Vectorexists since Java 1.0;
Listinterface exists since Java 1.2, and so does
Vectorhas been retrofitted to implement the
Listinterface at that same time;
- Java 5, introducing generics, has been introduced in 2004 (link).
Your course, dating back 2005, should have had knowledge of
ArrayList at the very list (sorry, least), and should have introduced generics too.
Array, there is
java.lang.reflect.Array, which helps with reflections over arrays (ie,
Vectorsynchronizes all operations, which is a waste in 90+% of cases;
- if you want concurrent collections, Java 5 has introduced
CopyOnWriteArrayListetc, you should use those;
- DO NOT use
Vectoranymore in any event; some code in the JDK still uses it, but it is for backwards compatibility reasons. In new code, there are better alternatives, as mentioned in the previous point;
- since Java 1.2,
Vectordoes not offer the same thread safety guarantees as it used to offer anyway.
The latter point is interesting. Prior to
Iterator there was
Enumeration did not offer the possibility to remove elements;
Iterator, however, does.
So, let us take two threads
Vector, and those two threads having an
Iterator over that vector. Thread
// remember: different iterator
With some unlucky timing, you have:
removes last element
.next() --> BOOM
Vector is in fact not thread safe. And it is even less thread safe since Java 5's introduction of the "foreach loop", which creates a "hidden" iterator.
Vector vs. ArrayList which is better?
As per this question
Vector is considered "obsolete", use