What is difference between Vector and ArrayList in Java?
- One of the important interview question in java specially from Experience candidate
/*
* Vector is synchronized and thread-safe.
* Performance wise slower then ArrayList.
* One Thread is running at a time.
* Once thread got completed it gives lock to other thread
* Thread Safe
*/
ArrayList :-
/*
* ArrayList is not synchronized nor thread-safe.
* Performance wise faster then Vector.
* multiple Threads is running at a time.
* Not Thread Safe
*/
Programme
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package highleveljava;
import java.util.*;
/**
*
* @author Admin
*/
public class ArrayListandVector {
public Vector vector;
public List list;
public String fname = "Ajeet";
public String tname = "Antrish";
public String lname = "Anand";
public void compareALVC() {
vector = new Vector();
/*
Add Some Data into Vector
*/
vector.add(fname);
vector.add(tname);
vector.add(lname);
System.out.println("Vector data is " + vector);
/*
* Vector is synchronized and thread-safe.
* Performance wise slower then ArrayList.
* One Thread is running at a time.
* Once thread got completed it gives lock to other thread
* Thread Safe
*/
list = new ArrayList();
/*
Add Some Data into ArrayList
*/
list.add(fname);
list.add(tname);
list.add(lname);
System.out.println("List data is " + list);
/*
* ArrayList is not synchronized nor thread-safe.
* Performance wise faster then Vector.
* multiple Threads is running at a time.
* Not Thread Safe
*/
}
public static void main(String[] args) {
ArrayListandVector classobject = new ArrayListandVector();
classobject.compareALVC();
}
}
Comments
Post a Comment