ArrayList vs Vector in Java
ArrayList Vs. Vector in Java
In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable differences in their implementation.
ArrayList
The built-in array available in Java has a drawback. We cannotchange its size once wehave declared it. ArrayList overcomes this drawback. ArrayList is a class declared in java.util package. It is resizable and can increment its size by 50% if the number of elements exceed.
The following operations can be performed on an ArrayList:
- Adding new elements to ArrayList
- Accessing elements of ArrayList
- Changing elements of ArrayList
- Removing elements from ArrayList
The following program demonstrates how to implementthe above operations,
SampleArrayList.java
import java.util.ArrayList; public class SampleArrayList { /* Driver Code */ public static void main(String ar[]) { /* ArrayList Creation */ ArrayList<String> cities = new ArrayList<>(); /* Adding elements to ArrayList */ cities.add("Kolkata"); cities.add("Delhi"); cities.add("Mumbai"); System.out.println("Initial ArrayList: " + cities); /* Adding elements using add() method */ cities.add(1, "Chennai"); System.out.println("After adding new element: " + cities); /* Accessing elements using get() method */ String c1 = cities.get(1); System.out.println("Accessing element at index 1: " + c1); /* changing the element using set() method */ cities.set(2, "Banglore"); System.out.println("After updating: " + cities); /* Removing element from index 2 using remove() method */ String str1 = cities.remove(2); System.out.println("After deletion: " + cities); /* Accessing elements of ArrayList using for-each loop */ System.out.println("Accessing individual elements: "); for (String c : cities) { System.out.print(c); System.out.print(" "); } } }
Output:
Initial ArrayList: [Kolkata, Delhi, Mumbai] After adding new element: [Kolkata, Chennai, Delhi, Mumbai] Accessing element at index 1: Chennai After updating: [Kolkata, Chennai, Banglore, Mumbai] After deletion: [Kolkata, Chennai, Mumbai] Accessing individual elements: Kolkata Chennai Mumbai
In the above code snippet, an ArrayListcities is declared inside SampleArrayList class and different operations are performed on it using the method available in theArrayList class.
Vector
The Vector class is just like the ArrayList but with a few differences. The size of the vector object is increased by double when new elements are added. All the methods in the Vector class are synchronized which means only one thread can work on the vector object at once. Hence it makes Vector slower as compare to the ArrayList.
The following operations can be performed on an ArrayList:
- Adding new elements to Vector
- Accessing elements of Vector
- Changing elements of Vector
- Removing elements from Vector
The following program demonstrates how to implement the above operations,
SampleVector.java
import java.util.Vector; import java.util.Iterator; public class SampleVector { /* Driver Code */ public static void main(String[] args) { /* Creating a Vector */ Vector<String>colors= new Vector<>(); /* Adding elements to Vector using add() method */ colors.add("Blue"); colors.add("Red"); /* Adding element using index number */ colors.add(2, "Yellow"); System.out.println("Vector: " + colors); /* Adding element using addAll() method */ Vector<String> colors2 = new Vector<>(); colors.add("White"); colors2.addAll(colors); System.out.println("New Vector: " + colors2); /* Accessing elements using get() method */ String element = colors.get(2); System.out.println("Element at index 2: " + element); /* Accessing elements using iterator() */ Iterator<String> iterate = colors.iterator(); System.out.print("Accessing individual elements: "); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(" "); } System.out.println(); /* Removing element using remove() method */ String element1 = colors.remove(1); System.out.println("Removed Element: " + element1); System.out.println("New Vector: " + colors); /* Removing all the elements of Vector using clear() method */ colors.clear(); System.out.println("Vector after clear(): " + colors); } }
Output:
Vector: [Blue, Red, Yellow] New Vector: [Blue, Red, Yellow, White] Element at index 2: Yellow Accessing individual elements: Blue Red Yellow White Removed Element: Red New Vector: [Blue, Yellow, White] Vector after clear(): []
In the above code snippet, a vector colors is declared inside SampleVector class and different operations are performed on it using the method available in the Vector class.
ArrayList Vs Vector
Sr. No. | ArrayList | Vector |
1. | An ArrayList is a non-synchronized data structure that is used to store elements asa dynamic array. | A Vector is a synchronized data structure that is used to store elements as a dynamic array. |
2. | The elements in the ArrayList are traversed using the Iterator interface. | The elements in Vector are traversed using the Iterator or Enumeration interface. |
3. | Increases the array size by 50% if a number of elements are incremented. | Increases the array size by 100% i.e., doubles, if number of elements are incremented. |
4. | ArrayList works faster compared to Vector. | Vector works slower compared to ArrayList |
5. | ArrayList is not a legacy class. | Vector is a legacy class. |
6. | ArrayList is used in single-user applications. | Vector is used in multi-user applications. |
7. | ArrayList is not Thread-safe because multiple threads can access the ArrayList simultaneously. | Vector is Thread safe because at any time only one thread is allowed to access the Vector object. |
In this article, we have discussed ArrayList and Vector classes in Java and also compared both these classes.