ArrayList and Vector both implements List interface and maintains insertion order.
1) ArrayList is not synchronized whereas Vector is synchronized.
2) ArrayList increments 50% of current array size if number of element exceeds from its capacity whereas Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
3) ArrayList is fast because it is non-synchronized whereas Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
4) ArrayList uses Iterator interface to traverse the elements whereas Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
ArrayList Example:
Vector Example :
}
1) ArrayList is not synchronized whereas Vector is synchronized.
2) ArrayList increments 50% of current array size if number of element exceeds from its capacity whereas Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.
3) ArrayList is fast because it is non-synchronized whereas Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.
4) ArrayList uses Iterator interface to traverse the elements whereas Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.
ArrayList Example:
import java.util.*;
class TestArrayList21{
public static void main(String args[]){
List<String> al=new ArrayList<String>();//creating arraylist
al.add("Sonoo");//adding object in arraylist
al.add("Michael");
al.add("James");
al.add("Andy");
//traversing
elements using Iterator
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
} Vector Example :
import java.util.*;
class TestVector1{
public static void main(String args[]){
Vector<String> v=new Vector<String>();//creating vector
v.add("umesh");//method of Collection
v.addElement("irfan");//method of Vector
v.addElement("kumar");
//traversing
elements using Enumeration
Enumeration e=v.elements();
while(e.hasMoreElements()){
System.out.println(e.nextElement());
}
}
No comments:
Post a Comment