Difference between ArrayList and Vector in Java
In Java interviews, they frequently ask what are all the differences between ArrayList and Vector, and often beginners get confused between these two. In this article, we are going to discuss all the differences between ArrayList and Vector in Java.
What is ArrayList in Java?
ArrayList is just like an array but with much more flexibility. In a traditional array, you specify the size, and if your input is larger than the array’s size, you’ll have to increase the size of your array, but in ArrayList, there’s no size limit. You get to add or remove elements anytime. It uses a dynamic array for storing the elements. This way it is much more flexible than the traditional array. It is a part of the Java Collection
framework and is a class of java.util.package
. Here, you do not need to specify the size (though you can do it if you want to).
Whenever you dynamically add or delete some elements from the ArrayList, the size of the array automatically increases. So, how exactly this happens? Here’s a rough idea – Whenever you add a new element in the ArrayList and its full-
- In the heap memory, it creates a bigger-sized memory
- In the new memory, it copies the current memory elements
- the new item gets added as we have bigger memory now
- deletes the old memory
ArrayList implements the List interface
and inherits the abstract class
. It allows us to randomly access any element. We need to use wrapper class
for ArrayList, primitive types like int
, char
, etc cannot be used.
For example, we created an ArrayList called names
that will store strings-
import java.util.ArrayList; // import the ArrayList class
ArrayList<String> names = new ArrayList<String>(); // Create an ArrayList object
Code language: Java (java)
What is vector in Java?
Vector is very useful when you want an array whose size changes over the lifetime of your program, or if you don’t know the size of the array. It is an implementation of List
interface. It allows us to create resizeable arrays (just like ArrayList). You can use any primitive type elements like- int, char, etc. It is found in java.util
package. The Vector class implements the List interface and is a child class of the AbstractList class. The capacity of the Vector class is dynamic in nature, which means you can shrink or increase it.
For example, we created a vector of String-
Vector<String> vec = new Vector<String>();
Code language: Java (java)
Difference between Vector and ArrayList
You must be wondering if both Vector and ArrayList are dynamic arrays and they both implement the List
interface, then what exactly is the difference between them? We’ll now discuss all the major differences between them.
Vector | ArrayList |
It is synchronized, which means only one thread can access the code at a time | It is not synchronized, multiple threads can work on ArrayList at the same time |
It increments 100% of the current array size if the number of elements exceeds the current array size | Whenever the number of elements exceeds the capacity of the array, it increments 50% of the current array size |
It is considered a legacy class | It was introduced in JDK 1.2 |
It is slower because of being synchronized | Since it is not synchronized, it is faster |
The iterator or Enumeration interface is used to traverse the elements | The iterator interface is used to traverse the array |
It defines the increment size | It does not define the increment size |
Conclusion
We discussed ArrayList and Vector in Java and all the differences between them. I hope you have understood the concept and can now easily distinguish between them.
Thanks for reading!
Sharing is caring
Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: