Differences Between Java Vector vs ArrayList
The one common definition we will remember while writing about vector(Java). Java vectors are commonly used instead of arrays. We can give examples for the same – While the data is increasing automatically it will expand. We can create a vector with initial size or with default initial size.
We will see the code for default initial size or initial size::
- To Create a Vector with default initial size
Vector v = new Vector();
- To Create a Vector with an initial size
Vector v = new Vector(300);
Below are the common vector methods::
Java ArrayList is one of the simplest and most used data structure in the implementation classes of the Java API library. The main important thing is it uses a dynamic array for storing the elements. One thing we should remember while creating arrays are those are of fixed length once these are created we cannot do any modifications.
Below are the Constructors
ArrayList( ) — It helps in building the empty array list
ArrayList(Collection c) — It helps in building a list of constructive elements in a
ArrayList(int capacity) –It helps in constructing an empty list.
Below are the most commonly used methods:: ::
- Boolean add(E obj)
- void add(int index, E obj)
- E get(int index)
- boolean contains(Object obj)
- boolean isEmpty()
Java was introduced with a new collection of API in Java 2.0 to provide the uniform structured classes. When new API introduced there will be changes in the methods. Now here we will see some of the old and new methods changed at the time of API change.
4.8 (3,306 ratings)
|Old Method||New Method|
|Object elementAt(int)||Object get(int)|
|void insertElementAt(Object, int)||void add(index, Object)|
|void addElement(Object)||boolean add(Object)|
|void setElementAt(int)||Object set(int, Object)|
|void removeElementAt(int)||void remove(int)|
|void removeAllElements()||void clear()|
Head To Head Comparison Between Java Vector vs ArrayList (Infographics)
Below is the Top 8 Comparison between Java Vector vs ArrayList
Key Differences Between Java Vector vs ArrayList
Below are the lists of points, describe the Key Differences Between Java Vector vs ArrayList
ArrayList is Non-synchronized and not thread safe but Vector is synchronized and having one thread to call methods at a time. But when coming to safety is single threaded case ArrayList is the only one choice but if we are working on multithreading then we need to prefer the Vectors. If we having any doubt or confusion in data then we can select vector because in vector we can set the increment value.
A vector can use both Enumeration and Iterator interface for traversing over elements but ArrayList can only use Iterator interface for traversing. In vector If you using the Integer wrapper, you will not be able to change the integer value. In the vector, the two most used methods have Next() and next(). ArrayLists are created with initial size because if objects are adding it will increase the size and objects are removing size will be decreasing automatically.
Vector class in Java implements a dynamic array of objects. It is exactly identical to the array, it contains elements accessible using a simple integer index. However, the size of a Vector can grow or shrink to accommodate adding and removing items on an as-needed basis.
Java Vector and ArrayList both provide a re-sizable array that means array that can increase the space. Java provides vector class to provide dynamic size, Generic and useful predefined methods (we already saw the methods in page1). Use an array if the size is fixed; use Vector if the size may change. Both Java Vector and ArrayList are index based and use the array internally and both Java Vector vs ArrayList maintain insertion order of elements. Java offers an ArrayList class to provide similar features as Vector dynamic, generic and useful predefined methods.
Java Vector and ArrayList both classes used for dynamic usage of arrays. Class ArrayList<E> — E specifies the type of objects that an array can hold. Here created array is variable one and it can increase or decrease based on the object allocation. Vector:: class Vector<E> — E represents the type of object that will be stored in the array.
Comparison Table Between Java Vector vs ArrayList
Following is the comparison table between Java Vector vs ArrayList
|Basis For Comparison||Vectors||ArrayList|
|Basic||Vector is the legacy class and it is synchronized||ArrayList is not the legacy class and it is not synchronized.|
|Size||Almost it will double the size if a total number of elements increased||ArrayList will increase by 50% of its size if the number of elements increased|
|Data Growth||Vector grow and shrink dynamically to maintain optimal use of storage||ArrayList grow and shrink dynamically to maintain optimal use of storage|
|Interface||Vector Uses Iterator and Enumeration interface||ArrayList Uses Iterator interface|
|Performance||Vector is slow when compared to ArrayList because it is synchronized||ArrayList is fast because of non-synchronization|
|Introduced in||Introduced in JDK 1.0 vertion||Introduced in JDK 1.2 vertion|
|Set Increment Size||Vector defines the increment size||ArrayList does not define the increment size|
Conclusion Java Vector vs ArrayList
Finally to conclude Java vector vs ArrayList both are having advantages and disadvantages. Both are very successful in the market. ArrayList is the recent once if we compare with Vector. ArrayList is preferred if you do not need synchronization. When using Vector or ArrayList always try to initialize the largest capacity that your program will need, because expanding the array is costly. I
ArrayList is synchronized why because more than one thread can be operated on the array at the same time With reference to the array if one thread is adding to the object another thread is removing an object from the same array on the same time.
Java Vector and ArrayList both hold object references. If the array is fully occupied and if we want to add new object after fully occupied, now in both the cases size will increase but the main difference comes in size in ArrayList if size is not specified it can increase by half of the current array but vector can double the size if the increment value is not specified.
I finally conclude by saying that always prefers to use ArrayList. Why because ArrayList performs faster and better when compared to Vector.
This has been a guide to Differences Between Java Vector vs ArrayList, their Meaning, Head to Head Comparison, Key Differences, Comparison Table, and Conclusion. You may also look at the following articles to learn more –
- Java Performance vs Python: What are the Differences
- Scala vs Java Performance: Which is the better ? (Infographics)
- Java vs Python – Top 9 important Comparisons You Must Learn
- Java Heap vs Stack – 7 Awesome Thing You Should Know
- C# Array vs List: What are the Benefits
- Array vs ArrayList | Top 6 Differences