ArrayList vs. Vector in Java: What's the Difference?

Advertisement

Key Difference

The difference between arraylist and vector in java is that in arraylist there is random access to the elements as it operates on the index based data structure whereas vector in java is a legacy class that is now reengineered to support the collection classes.

Advertisement

ArrayList vs. Vector in Java

Java is an object-oriented programming language that uses both compiler and interpreter. Mostly all software’s are made in Java programming language. Java code can be written on Windows, Linux, and Mac OS. The syntax of C and C++ programming language is quite the same. Java produces browser to run programs that help in making a graphical user interface. Java programming language is in used and trend these days. To write Java code, a programmer needs software development kit (SDK) that includes a compiler, interpreter that is not required in C++. If we talk about the main difference between arraylist and vector in java then the main difference between arraylist and vector in java is that in arraylist there is random access to the elements as it operates on the index based data structure whereas vector in java is a legacy class that are now reengineered to support the collection classes.

Advertisement

In arraylist, there is random access to the elements as it operates on the index based data structure. Arraylist is defined by the collection framework. The list is an interface that extends the collection interface. ArrayList is a collection of ordered element and elements are added, removed and accessed with the help of an index variable. There cannot be any duplicate elements in sets, but there can be duplicate elements in the list. There are some common methods in list that are index-based get() and set() method. Vector in java is a legacy class that is now reengineered to support the collection classes. List interface is implemented by the vector. Insertion order is also maintained in vector just like arraylist.  Vector vec =new Vector() this is the method that creates an empty vector, and there is an initial capacity of 10 in a vector in java. Following are methods in vector in java: void addElement(Object element), int capacity(), int size, void setsize(int size), Boolean contains(Object element), Boolean containsAll (collection C), Object firstElement(), Object last Element(), Object get (int index), Object isEmpty(). There are also add a () method that is used to add elements in the list, and another method removes () that is used to remove elements in the list. We can also say that list is a kind of array that can grow size as we add elements in the list. There is no method to set the range of indexed in a list. There is just a sub list() method that returns sub list from the original list of a specific range. The array list is implemented using Arraylist and can be accessed randomly. The dyanamic array is used in arraylist. Java arrayList is used for storing elements abstractionlist class and implements list interface. There are duplicate elements in Java ArrayList; Insertion order can be maintained by Java ArrayList. Java ArrayList is synchronized.

Advertisement

Comparison Chart

BasisArrayListVector
MeaningIn arraylist, there is random access to the elements as it operates on the index based data structureVector in java is a legacy class that are now reengineered to support the collection classes
Declaration Class declaration for ArrayList is class ArrayListClass declaration for Vector in java is class vector
Performance In Java arrayList is unsynchronizedIn Java Vector is synchronized.
Traverse Iteration is used for tranversal in ArrayListEnumeration is used for tranversal in Vector

 What is ArrayList?

In arraylist, there is random access to the elements as it operates on the index based data structure. Arraylist is defined by the collection framework. The list is an interface that extends the collection interface. ArrayList is a collection of ordered element and elements are added, removed and accessed with the help of an index variable. There cannot be any duplicate elements in sets, but there can be duplicate elements in the list. There are some common methods in list that are index-based get() and set() method. There are also add a () method that is used to add elements in the list, and another method removes () that is used to remove elements in the list. We can also say that list is a kind of array that can grow size as we add elements in the list. There is no method to set the range of indexed in a list. There is just a sub list() method that returns sub list from the original list of a specific range. The array list is implemented using Arraylist and can be accessed randomly. The dyanamic array is used in arraylist. Java arrayList is used for storing elements abstractionlist class and implements list interface. There are duplicate elements in Java ArrayList; Insertion order can be maintained by Java ArrayList. Java ArrayList is synchronized.

What is Vector?

Vector in java is a legacy class that are now reengineered to support the collection classes. List interface is implemented by vector. Insertion order is also maintained in vector just like arraylist.  Vector vec =new Vector() this is the method that creates an empty vector and there is a initial capacity of 10 in vector in java. Following are methods in vector in java: void addElement(Object element), int capacity(), int size, void setsize(int size), Boolean contains(Object element), Boolean containsAll (collection C), Object firstElement(), Object last Element(), Object get (int index), Object isEmpty().

Key Difference

  1. In arraylist there is random access to the elements as it operates on the index based data structure whereas Vector in java is a legacy class that are now reengineered to support the collection classes.
  2. Class declaration for ArrayList is class ArrayList whereas Class declaration for Vector in java is class vector.
  3. In Java arrayList is unsynchronized whereas i n Java Vector is synchronized.
  4. Iteration is used for tranversal in ArrayList whereas Enumeration is used for tranversal in Vector.

Explanatory Video