Wednesday, March 3, 2010

Java Collection Framework -1

I have interacted with several Java programmers with varying years of experience and have interviewed a few. Many of them who claim to have worked on Java technologies for years miss certain delicate aspect.

Here, I would try to walk you through a simple tutorial which will help you code effectively while using Java Collection Framework and avoid making not so obvious mistakes.


Collection as the name suggests is bag of objects/entities/elements. Collection framework is abstraction of this real life concept. The most popular Java collection is:

List : Which is an ordered collection of object. It is an abstract class and hence we initialise a List using ArrayList, Vector or LinkedList.

Here is sample code:

...
List list = new ArrayList();
list.add(new Integer(1));
...


Here we create a new list and add a Integer to it. One of the drawback of this implementation is I can further have :

...
list.add(new String("One"));
list.add(new Double(1.0));
...


The flexibility of having heterogeneous collection is prone to errors and misuse. Also rarely in real life we have heterogeneous collection. Until Java 1.4 there was no choice but to code as above but Java 1.5 introduced a feature of Generics. Generics imposes constraint of homogeneity over collection. As a result we can define Integer collection as:


...
List list = new ArrayList();
list.add(new Integer(1));
...


Now we would get compilation error :

...
list.add(new String(One)); //Compilation error
...


List is ordered collection which means, it retains the order in which objects are added to the collection. Following are key features of list:
1. Add one or more objects. (Only list allow adding elements at index)
2. Retrieve Object from given index.
3. Search for Object (uses sequential search)
4. Remove elements for given index.

At this point in time List may seem to be a effective data structure for most of the programming activity. One of the ingenious aspect of List is it does not have uniqueness constraint. Whenever we want to have collection of unique objects we can use Set instead of collection. Second big advantage of Set over List is : The performance of searching/finding an object is much faster in the former.

Here is some code:

...
Set set = new HashSet();
set.add(new Integer(1));
set.add(new Integer(2));
set.add(new Integer(3));
set.add(new Integer(1));
set.add(new Integer(2));

System.out.println(set.size()); //Prints 3.
...


Note that though we have added 5 element 2 of them are duplicates. So far everything seems easy and good....

Lets take a break and continue, in next post understanding more interesting facts with reference to collections.

1 comment:

  1. Hi Aniket, have been trying to get through to you to discuss on a startup that I and 2 of my batchmates from the 2007 IITB batch are running. We wanted to discuss with you on some IR,NLP related issues and also evaluate the possibility of a formal engagement. Do let me know your contact / drop me a mail at vivek@makesense.in. No other way to reach you so I thought I'd try this. Regards, Vivek Arya.

    ReplyDelete