Home » Java programming language

Differences between Vector and ArrayList in Java

Java Vector vs ArrayList: Here, we are going to learn about the differences between Vector and ArrayList in Java, compare Vector v/s ArrayList.
Submitted by Preeti Jain, on July 28, 2019

Vector vs ArrayList in Java

1) Vector in Java

  • Vector is an implementation class of List interface.
  • Vector is a legacy class that means it does not fully support the collection framework.
  • Vector class 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.
  • Vector is thread-safe that means only one thread is allowed to operate on one object that's why Vector is thread-safe.
  • We will see given how to create a Vector object with default constructor.
    Syntax:
    Vector v = new Vector();
  • The default size of the Vector object is 10 so if reached to its maximum capacity then-new capacity will be given below:
    New capacity = 2*Current_Capacity;
  • Vector class implements RandomAccess interface so that we can access random element with the same speed.

Example:

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());
    }
}

Output

E:\Programs>javac VectorClass.java
Note: VectorClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java VectorClass
Current Capacity:10
Capacity of adding till 10 elements is:10
New Capacity after adding 11th element will be 20

2)ArrayList in Java

  • ArrayList is an implementation class of List interface.
  • ArrayList is not a legacy class that means it supports the collection framework and we don't need to re-engineer this class.
  • When we create an ArrayList object then default capacity to store the element is 10 so if it reaches to its maximum capacity than a new ArrayList object will be created and capacity will increase like (half of its current capacity+1) of newly created object in case if we store 11th element in the Vector list the new capacity will be 16.
  • ArrayList is not thread-safe because it is not synchronized.
  • We will see, How to create an object of ArrayList by using ArrayList class.
    Syntax:
    ArrayList object_name = new ArrayList();
  • The default size of the ArrayList is 10.
  • Once the ArrayList reaches its maximum capacity then a new object will be created with new capacity and the new capacity will be calculated like this:
    New_capacity = Current_Capacity*3/2 + 1
  • The ArrayList class implements RandomAccess interface so that we can access random element with the same speed.

Example:

import java.util.*;

class ArrayListClass {
    public static void main(String[] args) {
        // Creating a ArrayList instance
        ArrayList al = new ArrayList();

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

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

        // Loop to print again 6 elements
        for (int j = 0; j < 6; ++j)
            al.add(j);

        // New capacity if it reached its maximum capacity
        System.out.println("New Capacity after adding 6 more element will be " + al.size());
    }
}

Output

E:\Programs>javac ArrayListClass.java
Note: ArrayListClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java ArrayListClass
Capacity of adding till 10 elements is:10
New Capacity after adding 6 more element will be 16






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.