Collections when and what to use

Lets see we can use which collections according to their characteristics :

ArrayList :

As ArraylList implements List interface. ArrayList allows random access and fast iteration. So ArrayList can be used when

1. We need random access to the element.
2. When we normally not required to remove the elements between the others as removing the element is not fast in ArrayList.

However the default initial capacity of ArrayList is very small i.e. 10 When we add more number of elements resizing of Array happens. To avoid this high cost of resizing, if we already know the size of ArrayList elements then its better to construct the ArrayList with that much initial capacity. If we do not know size of ArrayList then we should keep initial capacity high.

Vector :

Vector also allows random access, but is quite slower than the ArrayList as Vector has Synchronized methods. So Vector is preferred over the ArrayList when we need Synchronization , but if developers already to do Synchronization by their own then Vector synchronization not required.

Unlike ArrayList storage management in Vector is based on two factors such as initial capacity and capacityIncrement. Vector's storage size increases in chunks the size of capacityIncrement.

This can be used as ArrayList when you require synchronization.


If we check LinkedList class it implements interfaces such as List, Queue, Cloneable, Serializable. LinkedList contains nodes which are linked to each other. So along with actual data LinkedList also contain previous and next node links. When we add or remove any element surrounding nodes get updated automatically. In case of remove, the removed node gets garbage collected. So from this we can see that we can do fast manipulation with LinkedList but random access is quite slow.

1.As manipulating is very easy with LinkedList we can use it for iteration purpose and not for random access.
2. LinkedList can be used in operations involed such as stack, queue, or double ended queue (deque). As using LinkedList we can get, remove and insert an element at the beginning and end of the list.

Fast Random Access and Iteration.

Have synchronized method, slower than ArrayList

Used for adding elements to the ends i.e. Stacks and Queues.



HashSet implements Set interface. HashSet gives constant time performance for the basic operations such as add, remove and contains etc but iteration time depends upon the same two factors such as initial capacity and load factor. In HashSet
order of elements may or may not remain same.

So we will be using HashSet when
1. We need duplicate free collection.
2. When we need to find item in specific group or set. i.e. Which item is already being added etc.


Unlike the HashSet , elements in the TreeSet are sorted and have some methods like first(), last(), headSet(), and tailSet() to help ordering of elements more effective. But time required for basic operations such as add, size, contains etc is log(n). We cant manage the iteration performance of the TreeSet as it doesn't offer any parameters such as Initial Capacity and Load Factor.
We can use the TreeSet when fast access is not required and ordering is essential, but it is helpful when we add elements as HashSet and then convert it to TreeSet.


Like HashSet, LinkedHashSet gives constant time performance for operations such as add remove and contains. But LinkedHashSet maintains the order of the elements as it contains doubly linked list running through all entries. If we consider the iteration time taken by LinkedHashSet, this time here is directly proportional to the size of the set unlike from the HashSet in which it directly proportional to capacity. So if we want both ordering and fast access then LinkedHashSet is good choice.

Assures no duplicates, ordering is not provided, fast access.

Assures no duplicates, iteration by insertion.

Assures no duplicates, iteration by sorted order.

Hashtable :

Hashtable maps keys to values. In case of Hashtable any non null object can be stored as key or value. Hashtables are synchronized. The two factors which affect both Hashtable and HashMap are Initial Capacity and the load factor.(What is Initial Capacity and Load Factor? ).

1.When we need to store values in keys to values map then we can use Hashtable. Like "for a given X, what is the value of Y" i.e. For a given IP address get the Network Name.
2. If we need synchronization and not require null values as key.

HashMap :

HashMap is similar to the Hashtable only difference is HashMap is not synchronized and allow null values and null key.HashMap doesnt give guarantee of any order being maintained.

1. HashMap is used when we need key value pairs as discussed in Hashtable.
2. If we dont need synchronization and need null values and keys.


Synchronized, not allow null values.

Not synchronized, allow null values and keys.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


Post a Comment