Difference between vector and arraylist in Java
In this post, you will know the difference between vector and arraylist using the Java programming language.
Vector in Java
The vector class is used in Java to store data using the List interface. It can grow or shrink in size. It is an implementation of the List interface that allows us to create resizable-arrays. Vector implements a dynamic array, which means it can grow or shrink as required. The vector is synchronized, which means only one thread at a time can access the code. The vector class is generally used in the thread-safe implementation. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set().
Vector class Declaration
public class Vector<E>
extends Object<E>
implements List<E>, Cloneable, Serializable
ArrayList in Java
ArrayList is a part of the collection framework and is present in the java.util package. It is like an array, but there is no size limit. ArrayList is a dynamic array, and we do not have to specify the size while creating it. We can add or remove elements at anytime. ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time. It provides some useful methods like add(), addAll(), capacity(), clear(), clone(), elements(), equals(), size(), set(), remove(), trimToSize().
ArrayList Declaration
ArrayList<Type> arrayList= new ArrayList<>();
Difference between vector and arraylist
There are many differences between vector and arraylist. Here, we have mentioned most of them-
ArrayList | Vector |
ArrayList is not synchronized. | Vector is synchronized. |
ArrayList is not a legacy class. | Vector is a legacy class. |
ArrayList increments 50% of the current array size if the number of elements exceeds its capacity. | Vector increments 100% means doubles the array size if the total number of elements exceeds its capacity |
ArrayList is fast because it is non-synchronized. | Vector is slow because it is synchronized. |
ArrayList uses the Iterator interface to traverse the elements. | A Vector can use the Iterator interface or Enumeration interface to traverse the elements. |
Example of ArrayList in Java
import java.util.*;
public class JavaArrayList{
public static void main(String args[]){
//creating arraylist
List arrlist = new ArrayList();
//adding object in arraylist
arrlist.add("Smith");
arrlist.add("John");
arrlist.add("Any");
arrlist.add("Henry");
//traversing elements using Iterator
Iterator itr=arrlist.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output of the above code:
Smith
John
Any
Henry
Example of Vector in Java
import java.util.*;
public class JavaVector{
public static void main(String args[]){
Vector vec=new Vector();//creating vector
// adding object in vector
// using the add() method
vec.add("Smith");
// adding object in vector
// using the addElement() method
vec.addElement("Pepa");
vec.addElement("Jorz");
vec.addElement("Andy");
//traversing elements using Enumeration
Enumeration enm=vec.elements();
while(enm.hasMoreElements()){
System.out.println(enm.nextElement());
}
}
}
Output of the above code:
Smith
Pepa
Jorz
Andy
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java