Collection Framework - When to use What

Here we will talk about when to use what from the bunch we get in the collection framework. Also we will explore the reasons for using them.

Every class in the collection framework implements Cloneable and Serializable interfaces.

If you need to frequently access objects in your collection, then ArrayList and Vector are the best choices. Why? Both these classes implement the RandomAccess interface which provides them the ability to access any random element with the same speed.

ArrayList is a bad choice if we need to add or remove objects frequently. Reason being when you insert an object to an index, all the subsequent objects need to be displaced from their current positions. Secondly, everytime arraylist grows, it has to copy all the objects to the new list.

Vector is a legacy class. It is thread-safe, i.e. at a time only single thread can perform operations on it. Hence there is a performance toll when compared to ArrayList. ArrayList, on the other hand, is not thread-safe, so multiple threads can peform operations on it. If those are only retreival operations, the performance is very high in case of ArrayList.

If you need to frequently add/remove objects into the middle of the list, LinkedList is the best choice. Since internally it only needs to change a couple of pointers, its a very low cost operation.

LinkedList is a bad choice when you need to frequently access the objects. Why? The address of the next node lies only with the previous node. So you need to traverse a lot to find out its address and then access it.

HashSet should be used when you need to search for objects frequently. Because it stores based on hashcodes so they can be easily traced/searched. It is not a good choice when you need to frequently access the objects. ArrayList implemented RandomAccess but this one doesnt implement that.

Now if we want the insertion order to be preserved without the duplicate elements, we should go for LinkedHashSet.

TreeSet is suitable to use when we want to store the elements based on a sorting order. Duplicates not allowed, insertion order not preserved.

HashMap is best suited when we want to store key value pairs and when search is a frequent operation.

TreeMap is best when we want to store entries based on sorted order of keys.

Hashtable is a legacy interface based on hashtable structure itself. It is synchronized and should be used when search is required alongwith thread-safety.

Queues should be used when we need to store elements prior to some processing.

Collections and Arrays are classes that provide for sorting, searching, reversing, etc. We should utilize that rather than writing our own logic for such stuff.



Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures