CodingBison

If you are familiar with programming languages like "C", you would know that it provides all the basic entities like arrays, data-structures, functions etc that helps in storing/accessing/manipulating data. But, they typically do not provide libraries or API's that performs commonly used operations. For instance add, delete, deleteall, addall, iterateall, sort, search etc are some of the operations, that are used by most of the applications. It is up to an application to build the required operations, using the basic entities provided by "C" language.

In order to help the programmers build an application fairly easily, Java provides a framework that has defined/implemented tons of utility methods that help in manipulating the data, which is called Java Collection Framework. Java Collections Framework, is a collection of Interfaces (defines the methods) and classes (Implements the methods) that facilitates in storing/accessing of data easily. Further, it also provides a wide range of utility methods/operations that helps in manipulating the data.

Java Collections Framework

Java Collections Framework deals with "Collection". So what exactly is a collection? Well, a collection is an entity that represents a group of objects. In other words, a group of objects can be represented as a single entity called "collection". Java Collection Framework provides, interfaces that defines the functionality of a collections. Next, it provides classes that implements the functionality defined by the interface and finally, methods that act (operate) on the collection.

You must be wondering what is so special about a collection ? And how does it differ from an Array, which also represents a group of objects ? Well for starters, Here are some of the differences, the size of an array is fixed and cannot grow dynamically where as a collection can grow/shrink size dynamically. Since a collection can shrink or grow automatically there wont be any wastage of memory, For instance, if we have declared an array of size, say 10000 and only 5 spaces are used, then the rest of the memory is wasted. An array can store/represent any type of data (Objects and primitive data types) where a collection deals only with objects. The below table lists and compares various aspects of these 2 concepts.


Table: Comparison between an Array and a Collection.
ArrayCollection
Fixed Size.Flexible size.
Stores Primitive data types and Objects.Stores only objects.
Data is accessed using Array Index.Operations like add,delete etc are available.
No Underlying data structure.A collection has an underlying data structure.
No operations since no data structure.All the operations that comes with a standard data structure are available. Eg pop, push for a stack.
Faster and performs better.Slower compared to Arrays. All the operations like add, delete etc and the advantages like ease of use, flexibility, flexible size etc comes at the cost of performance.

Most of the common programming languages (like 'c') provide basic data-structures like array that helps us store/manipulate process data. Beyond that, they do not provide built-in-support for advanced data structures like tree, set, Hash tables etc.

Advantages of Collections

Java Collections Framework offers various advantages. First, is the ease of use of framework. All API's are simple and the look/feel of them are similar to the standard Java API's, Hence a Java programmer will be able to use them with little or no difficulty. Second, all the commonly used data structures and their operations are readily available and hence reduces the effort to build an application. Imagine if you were to implement and maintain all them as a part of your application! Next, Collections framework defines a list of interfaces and also their implementation classes. For example, sorting functionality provided by the framework uses a default sorting algorithm, but, if an application wants to use it own version of the algorithm, they could override the frameworks implementation and implement its own algorithm. Thus the framework provides the flexibility to the application developer. Lastly, all of the complexity that comes with the data structure operations are hidden, as the framework provides a set of wrapper API's that abstracts the complexity and also the underlying algorithm/data structures.

Java Collections Framework defines a wide variety of data structures like sets, list, maps, trees etc. We will see each of these interfaces, implementation classes and the operations that comes with them, in detail in the coming sections. Here is the figure that represents the full hierarchical relations between these interfaces Java Collections Hierarchy.

Collection and Inheritance

The Collections Framework relies upon one of the the key concepts of object oriented programming: inheritance. Inheritance defines the hierarchical relationships between various classes. An easier way to understand is that inheritance is like inheriting things/characteristics from our parents. With Collections Framework, the idea is same as the one we have, where sub-classes inherit things from the parent classes. Here is a simple illustration of a child inheriting money from the parent and then passing on to his own child.



Figure: Inheritance -- Child (object) inherits things from Parent (object)

With inheritance, a generic class aka super-class (in this case, the Collection class) is defined with a set of generic properties. This class is then inherited by myriad other collection-framework based sub-classes. These sub-classes automatically inherit methods and properties of the parent like the child in the above illustration inherits X1 amount of money. In addition to retaining methods/properties inherited, a sub-class can define its own specific properties like the child in above illustration adds X2 amount of money. With that, a sub-class can pick and choose methods/properties from its parent (or ancestor) class and also from the ones that it defines on its own.





comments powered by Disqus