What is the need of collection framework in java?

  • Arrays are not resizable.
  • Java Collections Framework provides lots of different useful data types, such as linked lists (allows insertion anywhere in constant time), resizeable array lists (like Vector but cooler), red-black trees, hash-based maps (like Hashtable but cooler).
  • Java Collections Framework provides abstractions, so you can refer to a list as a List, whether backed by an array list or a linked list; and you can refer to a map/dictionary as a Map, whether backed by a red-black tree or a hashtable.

In other words, Java Collections Framework allows you to use the right data structure, because one size does not fit all.


Several reasons:

  • Java's collection classes provides a higher level interface than arrays.
  • Arrays have a fixed size. Collections (see ArrayList) have a flexible size.
  • Efficiently implementing a complicated data structures (e.g., hash tables) on top of raw arrays is a demanding task. The standard HashMap gives you that for free.
  • There are different implementation you can choose from for the same set of services: ArrayList vs. LinkedList, HashMap vs. TreeMap, synchronized, etc.
  • Finally, arrays allow covariance: setting an element of an array is not guaranteed to succeed due to typing errors that are detectable only at run time. Generics prevent this problem in arrays.

Take a look at this fragment that illustrates the covariance problem:

  String[] strings = new String[10];
  Object[] objects = strings;

  objects[0] = new Date();  // <- ArrayStoreException: java.util.Date

Collection classes like Set, List, and Map implementations are closer to the "problem space." They allow developers to complete work more quickly and turn in more readable/maintainable code.