Krivalar Tutorials 
Krivalar Tutorials


Difference Between Vector and Arraylist

VectorArrayList
Vector is a dynamically growing array which increases it array size as and when elements are added. ArrayList is also a dynamically growing array.
Vector is synchronized. Vector can be used safely in scenarios in which multiple threads try to access it. Vector is locked for access by one thread at a timeArrayList is not synchronized. ArrayList is suitable for single threaded environments
Can be used in Multithreaded environements easily. In multi-threaded environments, please use the following to synchronize:
List arrayList = Collections .synchronizedList( new ArrayList(..));
Stack class is a derived class of VectorRoleList, AttributesList and RoleUnresolvedList are derived classes of ArrayList
Vector is a legacy class and has method to return Enumeration as well as Iterator/ListIterator interfaces to iterate elementsArrayList does not support Enumeration. Iterator/ListIterator solves the purpose.


Example - Java Program using Vector and ArrayList



import java.util.ArrayList;
import java.util.Vector;

public class VectorAndArrayList {
	public static void main (String args[]) {

		System.out.println("------ Vector -----");
		Vector vector = new Vector();
		vector.add("Red");
		vector.add("Green");
		vector.add("Blue");
		vector.add("Yellow");
		System.out.println(vector);
		vector.remove("Blue");
		System.out.println(vector);

		//For each element, form new string by concatenating Flower
		//and replace all elements with the new String
		vector.replaceAll(t -> (t + " Flower"));
		System.out.println(vector);

		System.out.println("------ ArrayList -----");
		ArrayList arrayList = new ArrayList();
		arrayList.add("Apple");
		arrayList.add("Orange");
		arrayList.add("Mango");
		arrayList.add("Grape");
		System.out.println(arrayList);

		arrayList.remove("Orange");
		System.out.println(arrayList);
		//For each element, take substring of first 4 characters
		//starting from index 0
		//and replace all elements with the new String
		arrayList.replaceAll(t -> (t.substring(0,4)));
		System.out.println(arrayList);

	}
}


The above program would display the result shown below:

------ Vector -----
[Red, Green, Blue, Yellow]
[Red, Green, Yellow]
[Red Flower, Green Flower, Yellow Flower]
------ ArrayList -----
[Apple, Orange, Mango, Grape]
[Apple, Mango, Grape]
[Appl, Mang, Grap]


<< abstract     final >>