java framework? - What are the differences between ArrayList and Vector?
stack than (7)
Vector both implements List interface and maintains insertion order.But there are many differences between
ArrayListis not synchronized.
ArrayListincrements 50% of current array size if number of element exceeds from its capacity.
ArrayListis not a legacy class, it is introduced in JDK 1.2.
ArrayListis fast because it is non-synchronized.
ArrayListuses Iterator interface to traverse the elements.
Vectorincrements 100% means doubles the array size if total number of element exceeds than its capacity.
Vectoris a legacy class.
Vectoris slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
Vectoruses Enumeration interface to traverse the elements. But it can use Iterator also.
What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
Basically both ArrayList and Vector both uses internal Object Array.
ArrayList: The ArrayList class extends AbstractList and implements the List interface and RandomAccess (marker interface). ArrayList supports dynamic arrays that can grow as needed. It gives us first iteration over elements. ArrayList uses internal Object Array; they are created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15.
Vector: Vector is similar to ArrayList but the differences are, it is synchronized and its default initial size is 10 and when the size exceeds its size increases to double of the original size that means the new size will be 20. Vector is the only class other than ArrayList to implement RandomAccess. Vector is having four constructors out of that one takes two parameters Vector(int initialCapacity, int capacityIncrement) capacityIncrement is the amount by which the capacity is increased when the vector overflows, so it have more control over the load factor.
Some other differences are:
Vector is a broken class that is not threadsafe, despite it being "synchronized" and is only used by students and other inexperienced programmers.
ArrayList is the go-to List implementation used by professionals and experienced programmers.
Professionals wanting a threadsafe List implementation use a
As the documentation says, a
Vector and an
ArrayList are almost equivalent. The difference is that access to a
Vector is synchronized, whereas access to an
ArrayList is not. What this means is that only one thread can call methods on a
Vector at a time, and there's a slight overhead in acquiring the lock; if you use an
ArrayList, this isn't the case. Generally, you'll want to use an
ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the
Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a
There are 2 major differentiation's between Vector and ArrayList.
Vector is synchronized by default, and ArrayList is not. Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method. Synchronized means : it can be used with multiple threads with out any side effect.
ArrayLists grow by 50% of the previous size when space is not sufficient for new element, where as Vector will grow by 100% of the previous size when there is no space for new incoming element.
Other than this, there are some practical differences between them, in terms of programming effort:
- To get the element at a particular location from Vector we use elementAt(int index) function. This function name is very lengthy. In place of this in ArrayList we have get(int index) which is very easy to remember and to use.
- Similarly to replace an existing element with a new element in Vector we use setElementAt() method, which is again very lengthy and may irritate the programmer to use repeatedly. In place of this ArrayList has add(int index, object) method which is easy to use and remember. Like this they have more programmer friendly and easy to use function names in ArrayList.
When to use which one?
- Try to avoid using Vectors completely. ArrayLists can do everything what a Vector can do. More over ArrayLists are by default not synchronized. If you want, you can synchronize it when ever you need by using Collections util class.
- ArrayList has easy to remember and use function names.
Note : even though arraylist grows by 100%, you can avoid this by ensurecapacity() method to make sure that you are allocating sufficient memory at the initial stages itself.
Hope it helps.
- Vectors are synchronized, ArrayLists are not.
- 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.
With a sorted array, the condition
data[c] >= 128 is first
false for a streak of values, then becomes
true for all later values. That's easy to predict. With an unsorted array, you pay for the branching cost.