Java - Difference between Vector and ArrayList

By Preeti Jain Last updated : February 03, 2024

Both Vector and ArrayList implement the List interface and maintain insertion order. They have several similarities and differences. In this tutorial, we will learn about the main differences between Vector and ArrayList.

What is Vector?

Vector is an implementation class of List interface. Vector is a legacy class that means it does not fully support the collection framework. It is introduced in earlier versions of java so these classes need to be re-engineered to support collection framework.

When we create a Vector object then default capacity to store the element is 10 so if it reaches to its maximum capacity then a new vector object will be created and capacity will be doubled of its current capacity of the newly created object in case if we store 11th element in the Vector list.

Package

The following package is required to use Vector:

import java.util.Vector;

Declare and initialize

The following is the declaration and initialization statement of Vector:

Vector<String> myVector = new Vector<>();

Example

This example demonstrates an example of Java Vector.

import java.util.*;

class VectorClass {
  public static void main(String[] args) {
    // Creating a Vector instance
    Vector v = new Vector();

    // Display default capacity of Vector object
    System.out.println("Current Capacity:" + v.capacity());

    // Loop to print 10 elements
    for (int i = 0; i < 10; ++i)
      v.addElement(i);

    // Display capacity of adding till 10 elements
    System.out.println("Capacity of adding till 10 elements is:" + v.capacity());

    v.addElement(11);

    // New capacity if it reached its maximum capacity
    System.out.println("New Capacity after adding 11th element will be " + v.capacity());
  }
}

The output of the above example is:

Current Capacity:10
Capacity of adding till 10 elements is:10
New Capacity after adding 11th element will be 20

What is ArrayList?

ArrayList class provides resizable arrays. It is a class of java.util package. ArrayList grows and shrinks dynamically during the runtime which makes it a more flexible data structure.

Package

The following package is required to use ArrayList:

import java.util.ArrayList;

Declare and initialize

The following is the declaration and initialization statement of ArrayList:

ArrayList<String> myArrayList = new ArrayList<>();

Example

This example demonstrates an example of Java ArrayList.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // Declare and initialize an ArrayList
    ArrayList < Integer > numbers = new ArrayList < > ();

    // Add elements
    numbers.add(11);
    numbers.add(23);
    numbers.add(45);
    numbers.add(12);
    numbers.add(67);

    // Display
    System.out.println("ArrayList:");
    for (int num: numbers) {
      System.out.println(num);
    }

    // Add an element at a specific index
    numbers.add(1, 100);

    // Display
    System.out.println("\nArrayList after adding one more element:");
    for (int num: numbers) {
      System.out.println(num);
    }

  }
}

The output of the above example is:

ArrayList:
11
23
45
12
67

ArrayList after adding one more element:
11
100
23
45
12
67

Difference between Vector and ArrayList

Here are the main differences between ArrayList and LinkedList in Java:

VectorArrayList
Vector is a legacy class that means it does not fully support the collection framework. ArrayList is not a legacy class.
Vector is synchronized. ArrayList is not synchronized.
Vector is slow because of synchronization. ArraList is faster because of non-synchronization.
The Vector is increased by a factor of 2 which means doubles its size if the current capacity of the vector increases. The ArrayList is increased by a factor of 1.5 which means half of its size if the current capacity of the ArrayList increases.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.