ArrayList
ArrayList is not synchronized because it can be used by two threads
simultaneously.
ArrayList is fast because it is non-synchronized
ArrayList uses Iterator interface to traverse the elements
Example:
import java.util.*;
public
class ArrayListDemo{
public static void main(String args[]){
ArrayList<String> li1=new
ArrayList<String>(); //Creating arraylist
li1.add("Dipak");//Adding object in
arraylist
li1.add("Debasish");
li1.add("harish");
li1.add("surya");
//Traversing list through Iterator
Iterator itr=li1.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
Dipak
Debasish
harish
surya
Vector
Vector is synchronized because it cannot be used by two threads
simultaneously.
Vector is slow because it is synchronized
Vector uses Enumeration interface to traverse the elements
Example:
import java.util.*;
public
class VectorDemo{
public static void main(String args[]){
Vector<String> ve1=new Vector<String>();//creating
vector
ve1.add("Dipak");//method of Collection
ve1.addElement("Debasish");//method of Vector
ve1.addElement("Harish");
//traversing elements using Enumeration
ve1.addElement("Surya");
Enumeration e1=ve1.elements();
while(e1.hasMoreElements()){
System.out.println(e1.nextElement());
}
}
}
Output:
Dipak
Debasish
harish
surya
Similar Between ArrayList and
Vector
ArrayList
ArrayList is a part of Collection
Java.util package is used in ArrayList
ArrayList provides dynamic arrays in Java
ArrayList inherits AbstractList class
and implements List interface
Vector
Vector is a part of Collection
Java.util package is used in Vector
Vector provides dynamic arrays in Java
Vector
inherits AbstractList class and implements List interface
No comments:
Post a Comment