Wednesday, September 22, 2010

Collection Framework in java

A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections. For more see what are collections?

Collection Framework

A Collections Framework mainly contains the following 3 parts -
  • Interfaces
  • Concrete class implementation of the iterfaces
  • Algorithms to deal with these classes and interfaces
A Collections Framework is defined by a set of interfaces, concrete class implementations for most of the interfaces and a set of standard utility methods and algorithms. In addition, the framework also provides several abstract implementations, which are designed to make it easier for you to create new and different implementations for handling collections of data. Collection framework part 1 : Core Collection Interfaces The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation. The 6 core Interfaces used in the Collection framework are:
  • Collection
  • Iterator
  • Set
  • List
  • SortedSet
  • Map
  • SortedMap
  • Queue (Added in jdk 5)
  • Deque (Added in jdk 5)
Collections also provide implementation for these interfaces.
collectionFramework In java 5, queue was also provided. From the figure we get that in the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of map is to provide access to values stored by keys. When designing software with the Collection Framework, it is useful to remember the following hierarchical relationship of the four basic interfaces of the framework.
  • The Collection interface is a group of objects, with duplicates allowed.
  • Set extends Collection but forbids duplicates.
  • List extends Collection also, allows duplicates and introduces positional indexing.
  • Map extends neither Set nor Collection
  • Iterator is not part of collection

Collection Framework part 2 : Interfaces and their implementations

These are concrete implementations of the collection interfaces. In essence, these are reusable data structures.
Interface Implementations
Array Balanced Tree Linked List Hash table Historical
List ArrayList LinkedList Vector, Stack
Map TreeMap HashMap HashTable,
Set TreeSet HashSet
Deque ArrayDeque LinkedList
The historical collection classes are called such because they have been around since 1.0 release of the java class libraries. If you are moving from historical collection to the new framework classes, one of the primary differences is that all operations are synchronized with the new classes. While you can add synchronization to the new classes, you cannot remove from the old. See here - list of interfaces and classes in collections

Collection Framework part 3 : Algorithms

These methods that perform useful computations, like searching and sorting, on objects that implement collection interfaces. These algorithms are said to be polymorphic because the same method can be used on many different implementations of the appropriate collections interface. In essence, algorithms are reusable functionality.

No comments:

Post a Comment