Arraylist Vs Vector Vs LinkedList

January 22, 2016 , 0 Comments

java-collection-hierarchy

 































































 ArrayList LinkedList
ImplementationResizable ArrayDouby-LinkedList
ReverseIteratorNoYes , descendingIterator()
Initial Capacity10Constructs empty list
get(int) operationFastSlow in comparision
add(int) operationSlow in comparisionFast
Memory OverheadNoYes

When to Use ArrayList and LinkedList :

In real world applications , you will more frequently use ArrayList than LinkedList. But in a very specific situations LinkedList can be preferred.

1. ArrayList is preferred when there are more get(int) or search operations need to be performed as every search operation runtime is O(1).

2. If application requires more insert(int) , delete(int) operations then the get(int) operations then LinkedList is preferred as they do not need to maintain back and forth like arraylist  to preserve continues indices.

Similarities between ArrayList and LinkedList :

1. Not synchronized :  Both ArrayList and LinkedList are not synchronized ,  and can be made synchronized explicitly using Collections.synchronizedList() method.

2. clone() operation :  Both ArrayList and LinkedList returns a shallow copy of the original object ,i.e.  the elements themselves are not cloned.

3. Iterators : The iterators returned by ArrayList and LinkedList class's iterator and listIterator methods are fail-fast. Fail fast iterators throw ConcurrentModificationException . We have already discussed the difference between fail-fast and fail-safe iterators.

 

arraylist-vs-linkedlist1

arraylist-vs-linkedlist-complexity

ArrayList Vs Vector:


1) Synchronization: ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. For e.g. if one thread is performing an add operation on ArrayList, there can be an another thread performing remove operation on ArrayList at the same time in a multithreaded environment

while Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Unlike ArrayList, only one thread can perform an operation on vector at a time.

2) Resize: Both ArrayList and Vector can grow and shrink dynamically to maintain the optimal use of storage, however the way they resized is different. ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.

3) Performance: ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe, the thread which works on Vector gets a lock on it which makes other thread wait till the lock is released.

4) fail-fast: First let me explain what is fail-fast: If the collection (ArrayList, vector etc) gets structurally modified by any means, except the add or remove methods of iterator, after creation of iterator then the iterator will throw ConcurrentModificationException. Structural modification refers to the addition or deletion of elements from the collection.

As per the Vector javadoc the Enumeration returned by Vector is not fail-fast. On the other side the iterator and listIterator returned by ArrayList are fail-fast.

5) Who belongs to collection framework really? The vector was not the part of collection framework, it has been included in collections later. It can be considered as Legacy code. There is nothing about Vector which List collection cannot do. Therefore Vector should be avoided. If there is a need of thread-safe operation make ArrayList synchronized as discussed in the next section of this post or use CopyOnWriteArrayList which is a thread-safe variant of ArrayList.

Simplified Answer :

Arraylist :     Asyncronized methods , Faster than Vector ,Increase size automatic, Search Functionlity faster than others

Vector   :       Syncronized Mathods,Slower than other collection, Increase Size as per demand,

LinkedList : Insert and Delete Functionality is faster than arraylist.

 

 

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *