Java ArrayList vs Vector

In short, normally, use ArrayList. The difference between two:
  • ArrayList is faster
  • Vector is synchronized, any method that touches the Vector's content is thread safe. But ArrayList is not
  • Both ArrayList and Vector internally store data using Array. When it need to grow (by adding element), ArrayList increases 50% of the original size, Vector doubles the size of the array, and Vector can set the increment value.

Reference:
Vector or ArrayList -- which is better?

ArrayList Usage:
import java.util.ArrayList;

public class prnote {
    public static void main( String[] args ) {
        ArrayList<String> arr = new ArrayList<String>();
        arr.add( "one" );
        arr.add( "two" );
        System.out.println( arr.get( 0 ) );
    }
}

Vector Usage:
import java.util.Vector;

public class prnote {
    public static void main( String[] args ) {
        Vector<String> v = new Vector<String>();
        v.add( "one" );
        v.add( "two" );
        System.out.println( v.get( 0 ) );
    }
}

No comments:

Post a Comment