ArrayList is Resizable-Array implementation of the List interface and introduced in 1.2 version. It allow all elements include null. ArrayList is unsynchronized and it is not thread safe.In this class size,isEmpty,get,set,Iterator and ListIterator operations run in constant time.The add operation run in amortized constant time,that is,adding n elements requires O(n) time. Each ArrayList instance has a capacity.The capacity is the size of the array used to store elements in the list.An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation.
Vector class is implements a growable array of objects.The size of a vector can grow or shrink as needed to accommodate adding and removing items after the vector has been created.Each vector maintaining a capacity and a capacityIncrements.
Difference between ArrayList and Vector:
1. ArrayList class methods are not Synchronized and Vector class methods are Synchronized.So Vector class is used in multi-threading and concurrent environment.
2. When Vector class reaches it's threshold limit,then it automatically increase itself by value specified in capacityIncrement. But ArrayList call ensureCapacity() method to increase its size.
3. ArrayList gives better performance than Vector.Because ArrayList is not Thread-Safe but Vector is Thread-safe that's why it give low performance.
4. ArrayList is introduced in 1.2 version but Vector is introduced in 1.0 version and it is called as Legacy class.
5. Vector class is Legacy class,we use Enumeration class to print elements of Vector class.For ArrayList we use Iterator for printing elements of ArrayList objects.
If your project requirement is Threadsafe use Vector class otherwise use ArrayList class.Because ArrayList not Thread-safe so it gives better performance than Vector class.
Can we get Synchronized ArrayList:
Yes,We can get by using Collections.synchronizedList(List l); method.
ArrayList arrayList=new ArrayList();
List l = collections.synchronizedList(arrayList);
see also this link :
prime number
why array index start with zero
reverse string in different ways in java
see also this link :
prime number
why array index start with zero
reverse string in different ways in java
@Ram nice explanation
ReplyDelete