Quick overview of collections

Most used Interfaces in Collection Framework

 

Ordered collection


It maintains the order of the elements based on the sequence you add/remove the element to/from the collection.

Sorted collection


A sorted collection keeps the elements sorted based on sort criteria.

1. Set


  1. It is bothered about Uniqueness i.e. no duplicates are allowed.
  2. Here equals method is used to determine whether 2 objects are equal or not.
  3. Please refer key methods of Set interface here
Most used Implementations of Set interfaces
1.1   HashSet

  1. It’s unsorted and unordered Set.
  2. It uses hashCode of element being inserted in order to check whether element is duplicate. So more efficient your hashCode implementation better access performance you will get.
  3. When you iterate through it order is unpredictable.
  1. When to Use:
    1. You can use it, when you are not bothering about order of elements while iterating.
1.2   LinkedHashSet

  1. It’s an order version of HashSet that maintains doubly linked list across all the elements.
  2. It lets you to iterate through elements in the order in which elements they were inserted.
  1. When to Use:
    1. Use LinkedHashSet instead of HashSet when you bother about order of elements while iterating.
1.3   TreeSet

  1. It’s a sorted collection.
  2. It uses Red-Black tree structure and guarantees that the elements will be in ascending order according to natural order.
  3. Optionally when you construct TreeSet, its constructor lets you customize sorting by providing option of comparator or comparable.
  1. When to Use:
    1. Use TreeSet when you want elements in set to be sorted.

2. List


  1. It is bothered about index.
  2. List implementations are ordered by index position.
  3. A position that you determine either by setting an object at specific index or by adding it without specifying the position (in this case object will be added to the end.)
  4. Please refer key methods of List interface here
Most used Implementations of List interfaces
2.1   ArrayList

  1. Think it as growable Array.
  2. It is ordered collection.
  3. From Java version 1.4, ArrayList class implement’s RandomAccess marker interface in order to support fast random access of elements.
  1. When to Use:
    1. Use ArrayList over LinkedList when you need fast iteration.
2.2   Vector

  1. Vector is same as an ArrayList except all methods of Vector are Synchronized for thread safety.
  2. Vector is only class other than ArrayList to implement RandomAccess interface.
  1. When to Use:
    1. Prefer Vector instead of ArrayList when thread safety is required.
2.3   LinkedList

  1. LinkedList is ordered by index position like ArrayList except that elements are linked with each other using doubly linked list.
  2. When to Use:
    1. LinkedList may iterate slowly than ArrayList but it’s a good choice when you need fast insertion and deletion elements.

3. Map


  1. It is bothered about unique identifier.
  2. You can map unique key to specific value.
  3. It allows you to perform search based on key.
  4. Here equals method is used to determine whether 2 keys are equal or not.
  5. Please refer key methods of Map interface here
3.1   HashMap

  1. It’s unsorted and unordered Map.
  2. It uses hashCode of key to store the element. Hence the performance depends on hashCode
  3. It allows one Null key and multiple Null values.
  1. When to Use:
    1. You can use it, when you are not bothering about order for iteration of Map.
3.2   HashTable 

  1. It’s unsorted and unordered Map.
  2. HashTable is same as a HashMap except all methods of HashTable are Synchronized for thread safety.
  3. Null key is not allowed in HashTable
  1. When to Use:
    1. Prefer HashTable instead of HashMap when thread safety is required.
3.3   LinkedHashMap

  1. It’s an ordered Map as it maintains insertion order.
  2. It’s slower than HashMap for adding and removing element. But for iteration it is faster than HashMap
  3. When to Use:
    1. When you want to maintain the insertion order while iteration of Map then LinkedHashMap is good choice.
3.4   TreeMap

  1. It’s a sorted Map.
  2. It uses Red-Black tree structure and guarantees natural ordering of keys.
  3. When to Use:
    1. Use TreeMap when you want keys in Map to be sorted.

Happy learning !

Leave a Reply

Your email address will not be published. Required fields are marked *