ArrayList and Vector
While ArrayList and Vector both are child of List,
They differ from each other in some aspectSimilarity:-
They differ from each other in some aspectSimilarity:-
- Both implements RandomAccess interface.
- Both preserves the insertion order
- Both internally uses Array to store the elements.
- Both are slower than array.But working with direct array will be little difficult
As Both implements RandomAccess interface,the time taken to access 0th element and the time taken to access nth element will be nearly same.So Access time is constant.
However if you insert/delete element from the front/middle,lots of shift operation has to take place,which makes the ArrayList and Vector slow.
In such cases,it is advisable to use LinkedList.
In such cases,it is advisable to use LinkedList.
ArrayList:-
- It is not synchronised ,as the methods of ArrayList is not synchronized,
- Use ArrayList,If multiple thread will access it only to read data
- Donot use ArrayList ,if multiple concurrent thread are going to perform read and write operation on it
- Can be accessed using iterator,ListIterator,for each
- Does not support Enumeration
- Implements RandomAccess interface
- We can not get the capacity of the ArrayList,We can only get size of ArrayList
- Fast performance if used with multiple thread
- When ArrayList size becomes full,and it cannot hold more elements,and you try to add new element to the list,it dynamically increases the size by 50% of the current size.
Vector:-
- It is synchronized,as all the methods of Vector is synchronized.
- Use Vector,If multiple thread will access vector to read and write data.
- Slower than ArrayList due to synchronized behaviour.
- Can be traversed using Enumeration,Iterator,ListIterator.
- supports Enumeration,iteration,ListIterator,for each
- implements RandomAccess interface
- We can get the capacity of the vector as well as size
- When Vector size becomes full,and it cannot hold more elements,and you try to add new element to the vector,it dynamically increases the size by 200% thereby doubling it.
| ARRAYLIST | VECTOR |
|---|---|
| Not Synchronised | Synchronized |
| It increases its size everytime by 50% | Its size increases every time by doubling it |
| Enumeration not supported | All traversal interface supported |
Thank You for reading this article.Any bug or suggestion,We would like to hear from you.
Any thing missing?Plz reply as a comment
No comments:
Post a Comment