Wednesday, March 28, 2007

Collection framework in Java

  • Group of objects are handled using collection framework.
  • It allows different type of collections (dynamic arryas, Linked List, Trees and hash tables) to work in a similar manner with interoperability.
  • Some partial implementations are provided to create own collection.
  • Several implementations (such as LinkedList, HashSet and TreeSet) are provided to use.
  • Mechanisms were added that allow the integration of standard arrays into the collection framework.

  • A interface Iterator is created by collection framework which provides a means of enumerating the contents of collection.
  • Since each collection implements Iterator, the elements of collection class can be accessed through the methods defined by Iterator.
  • In addition to collections, the framework defines several Map interfaces and classes.
  • Maps store key/value pairs.
  • Maps are not collection but a collection-view of a map can be obtained and thus contents of a map can be accessed as collection.

The collection framework defines following Interfaces:

  1. Collection Top of the collection hierarchy.
  2. List Extends Collection to handle sequnces (lists of objects)
  3. Set Extends Collection to handle sets (contain unique elements)
  4. SortedSet Extends Set to handle sorted sets.
  • Collections that supports the methods which enable to modify the contents of it are called modifiable otherwise unmodifiable.
  • If such method is applied to unmodifiable collection, an UnsupportedOperationException is thrown.
  • All the build-in collections are modifiable.
  • If a collection can't be modified then UnsupportedOperationException is thrown and a ClassCastException is generated when one object is incompatible object to a collection.