Collections - sivakrsna/Java-Interview GitHub Wiki

Collection

  • A collection is simply an object that groups multiple elements into a single unit.
  • Collections are used to store, retrieve, manipulate, and communicate aggregate data.

Collection : Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), ... etc.

Set : Doesn't allow duplicates. Example implementations of Set interface are HashSet (Hashing based) and TreeSet (balanced BST based). Note that TreeSet implements SortedSet.

List : Can contain duplicates and elements are ordered. Example implementations are LinkedList (linked list based) and ArrayList (dynamic array based)

Queue : Typically order elements in FIFO order except exceptions like PriorityQueue.

Deque : Elements can be inserted and removed at both ends. Allows both LIFO and FIFO.

Map : Contains Key value pairs. Doesn't allow duplicates. Example implementation are HashMap and TreeMap. TreeMap implements SortedMap.