ArrayList & Vector in Java

ArrayList and Vector class in Java implements RandomAccess interface so that any random element access with the same speed.

ArrayList in Java

ArrayList is a part of the Java collection framework. ArrayList is available in the Java—util package. ArrayList is a dynamic array so we can increase or decrease the size according to requirement. ArrayList is not synchronized and due to this ArrayList performance is high.

Vector in Java

Vector is also part of the Java collection framework and is available on java. util package. it is also a dynamic array, the only difference with ArrayList is that the vector is synchronized due to this vector performance is relatively slow.

Differences between ArrayList and Vector

ArrayListVector
All methods present in ArrayList are non-Synchronized.All methods presented in the vector are Synchronized.
Multiple threads are allowed to operate on ArrayList at a time, hence ArrayList is not thread safe.Only one thread is allowed to operate on ArrayList at a time, hence vector is thread-safe.
Relatively performance is high because multiple threads can operate at a time and threads are not required to waitRelatively performance is low because only one thread can operate at a time and threads are required to wait.
Introduced in Java in 1.2v Introduced in Java in 1.0v

How to get the Synchronized Version of ArrayList Object

ArrayList is non – synchronized but we can get a synchronized version of the ArrayList object by using the SynchronizedList method of the collection class.

public static <T> java.util.List<T> synchronizedList(     @NotNull  java.util.List<T> list )

Example

import java.util.Collections;
import java.util.List;

public class ArrayList {
    public static void main(String[] args) {
        ArrayList l = new ArrayList();
        List l1 = Collections.synchronizedList(l);

    }
}

Similar Java Tutorials

Leave a Comment