Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is ArrayList?
1.1.
Program:
1.2.
Java
2.
What is Vector?
2.1.
Program: 
2.2.
Java
3.
Difference Between ArrayList and Vector 
4.
Key Differences between ArrayList and Vector
5.
Frequently Asked Questions
5.1.
What is the difference between stack and Vector in Java?
5.2.
Why vector is used in Java?
5.3.
Why is vector better than ArrayList?
6.
Conclusion
Last Updated: Jun 5, 2024
Easy

Difference Between ArrayList and Vector in Java

Author Sohail Ali
2 upvotes
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

ArrayList and Vector are both classes in Java that implement dynamic arrays, allowing flexible resizing. ArrayList is unsynchronized, faster, and grows by 50%, while Vector is synchronized, grows by doubling, and is legacy. ArrayList is generally preferred for better performance in single-threaded environments, whereas Vector is suitable for multi-threaded scenarios to prevent concurrent access issues.

Both are super easy and fast to implement and use. ArrayList and vectors are dynamic arrays. Dynamic simply means we can change their sizes whenever we want. Both of these classes are used to implement the list interface. 

Difference between ArrayList and Vector in Java

Now let's see what ArrayList and vectors are and what the difference is between ArrayList and vector.

Also read, Duck Number in Java

What is ArrayList?

An ArrayList is a resizable array that is a part of the java.util.package, and it is used to implement a list interface. As it helps create a dynamic array, the size is not required to be specified during creation. 

As we add or remove elements from ArrayList, the size of ArrayList changes accordingly. 

If the number of elements extends the capacity of ArrayList, the new size of ArrayList will increase by 50% of the present array size.

The syntax for the creation of ArrayList is as follows:

ArrayList<T> variable_name = new ArrayList<T>();


ArrayList is unsynchronized in nature. ArrayList allows multiple threads to work at the same time. Thus if a thread performs a task, the other thread doesn't need to wait to complete that task.

Program:

  • Java

Java

import java.util.*;
class ArrayList_Example{
public static void main(String[] args){
       // Creating an ArrayList of Integers
       ArrayList<Integer> arr = new ArrayList<Integer>();
       System.out.println("Initial size of arr: " + arr.size());
       for (int i = 0; i < 5; i++){
           // Adding element in the ArrayList
           arr.add(i);
       }
      
       System.out.print("After addition arr contains : ");
       System.out.println(arr);
       System.out.println("Size of S1 after addition: " + arr.size());
      
       // Removing elements from ArrayList
       arr.remove(4);
       arr.remove(3);
       System.out.print("Arr after removing elements: ");
       System.out.println(arr);
       System.out.println("Size of arr after deletion: " + arr.size());
   }
}

Output: 

ArrayList output

Explanation

In the above code of ArrayList in Java, the size of dynamically created array arr was initially zero, but whenever we added elements or removed some elements from the array, the size changed accordingly.

Also see,  Swap Function in Java

What is Vector?

Vectors are very similar to ArrayList. Both use dynamically resizable arrays internally, and both implement the list interface. Vector has some legacy methods that are not present in the collection framework. One significant difference between ArrayList and Vector in Java is if the elements inside the vector exceed its capacity, the vector increments the size by 100%, doubling the array's size.

The syntax for the creation of ArrayList is as follows:

Vector<T> variable_name = new Vector<T>();


Another difference between ArrayList and Vector is the vector is synchronized in nature. Multiple threads cannot work on vectors at the same time. Therefore if a thread is performing a task, the other thread has to wait to complete that task.

Program: 

  • Java

Java

import java.util.*;
class Vector_Example{
public static void main(String[] args){
       // Creating a vector of Integers
       Vector<Integer> V = new Vector<Integer>();
       System.out.println("Initial size of vector: " + V.size());
       for (int i = 0; i < 5; i++){
           // Adding element to the vector
           V.add(i);
       }
      
       System.out.print("After addition Vector contains : ");
       System.out.println(V);
       System.out.println("Size of vector after addition: " + V.size());
       System.out.println("Capacity of vector after addition: " + V.capacity());
      
       // Removing elements from vector
       V.remove(4);
       V.remove(3);
       System.out.print("Arr after removing elements: ");
       System.out.println(V);
       System.out.println("Size of arr after deletion: " + V.size());
   }
}

Output: 

Vector in Java output

Explanation

In the above code of vectors in Java, whenever we add elements to the vector its size gets changed respectively. Another important point to note is that whenever the vector reaches its capacity then the new capacity of the vector gets doubled to its current size.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Difference Between ArrayList and Vector 

So, now that you have understood the ArrayList vs  Vector in Java. Let's now see the comparison between both. 

ArrayList  Vector
An ArrayList is not synchronizedVectors are synchronized
As it is not synchronized, ArrayList is fastVector is slow compared to ArrayList, as it is synchronized
Multiple threads are allowed in ArrayListOnly a single thread is allowed in Vector
The performance of ArrayList is highThe performance of Vector is low compared to ArrayList
ArrayList is not a legacy and an inheritance classVector is a legacy as well as an inheritance class
ArrayList only uses iterators for traversing Vectors can use iterators and enumerations for traversing
ArrayList increases 50% of its current size in case the number of elements increases its capacity Vector grows 100% of its current size if the number of elements outgrows its capacity

Key Differences between ArrayList and Vector

The significant differences between ArrayList and Vector are:

1. Thread Safety:

  • ArrayList is not synchronized by default, making it faster in single-threaded environments.
  • Vector is synchronized, ensuring thread safety but potentially slowing performance.

2. Performance:

  • ArrayList generally performs better than Vector due to its lack of synchronization.
  • Vector's synchronization overhead can impact performance, especially in multi-threaded scenarios.

3. Growth Rate:

  • ArrayList increases its size by 50% when it reaches its capacity.
  • Vector doubles its size when it exceeds its capacity.

4. Legacy Status:

  • Vector is a legacy class, while ArrayList is part of the Java Collections Framework introduced later.

5. Iterator Fail-Fast:

  • ArrayList's iterator does not throw ConcurrentModificationException by default.
  • Vector's iterator throws ConcurrentModificationException if the vector is structurally modified.

6. Usage:

  • ArrayList is preferred in single-threaded applications or when thread safety is not a concern.
  • Vector is suitable for legacy codebases or scenarios where thread safety is crucial.

Frequently Asked Questions

What is the difference between stack and Vector in Java?

Stack is a subclass of Vector with specific stack operations, while Vector is a general-purpose collection class in Java.

Why vector is used in Java?

Vector is used in Java for thread-safe operations and legacy compatibility.

Why is vector better than ArrayList?

Vector ensures thread safety and is suitable for legacy code, while ArrayList is faster and preferred in single-threaded environments.

Conclusion

This article discusses the difference between ArrayList and Vector in Java. We hope this blog has helped you enhance your knowledge of ArrayList and Vectors and the similarities and differences between ArrayList and Vector in Java. If you want to learn more, then check out our articles.

And many more on our platform Code 360.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Previous article
How to Add Date in Arraylist Java
Next article
Difference Between List and ArrayList in Java
Live masterclass