Tuesday, 30 September 2014

Collections in Java :Part 1


Collections is a very special class in java. It includes collection framework which is classified into List, Set and Queue.
Let’s discuss an overview first, then in detail!

Collections includes three things .
                    1) List
                    2) Set 
                     3)Queue
Someone may remember a term Map right now. It doesnot belongs to collections however,we discuss it later.

List include three things.They are
                  1)Array List
                  2)LinkedList
                  3)Vector
                       -Stack

Set Includes:
                   1)Hash Set
                          -Linked Hash Set
                   2) Sorting Set
                           -Navigation Set
                            -TreeSet

Queue Includes
                    1) Priority Queue
                    2) Blocking Queue
                           -Priority Blocking Queue
                          -Linked Blocking Queue

Main use of collections is :

It is used for data transfer ie it holds the transfer the object .

Requirements to transfer the object :

1)To transfer the objects,they should be serializable

Here in collections, all objects are already serializable .As well they are clonable .

What is mean by clonable ?

When the objects are transferred from one place to other,they are not disturbed at the other end. At the other end they are cloned and operations are done on the cloned objects .So any misdeeds will not affect the original objects.

List

List is used when the order of insertion is very significant and duplication is not at all a problem.

Why we are going for Array List from Array ?

First let me explain ,Why we are using Array. 

To store one value we use the syntax as ,
datatype variable_name=1;
ie a=1;
To store another value, we can go for 
b=1;
But what if we want to store some 5000 values .Is it a best practice ? No. It is a worst practice to use the separate variables to store the values. So we are going for array .

Then why ArrayList ? Yes consider a situation when you declared a variable and stored 5000 values in array ie a[5000].But just think,what if you want to add another value to that particular array.Here comes a problem and the solution exists with the Array List. Yes ArrayList is extendable or growable in nature so its an easy task to add any element after some time .
Ex: ArrayList al=new ArrayList()

Three important constructors used in Array List :

ArrayList al=new ArrayList();
ArrayList al = new ArrayList(int Capacity); 
ArrayList al=new ArrayList(Collecton C); 

If you dont know the count to be used ,then arraylist is the excellent choice since it is growable. But nothing in this world comes with free of cost right ? Yes of course . It applies here too. 

When you insert some element in the middle of the Array List,then it replicates another list by including the new element.This leads to the performance degradation. 

Do you know,what will be the capacity of the new list formed ? 
You can calculate it with the following formula.

New Capacity = (Current Capacity*3/2) + 1

So if you know the capacity in advance,better use the constructor ArrayList al=new ArrayList(int Capacity) to mention the capacity in advance.

The constructot ArrayList al=new ArrayList(Collection C) is used for conversion purpose .

Other interesting fact about ArrayList is that,they implements random access interface .

Only ArrayList and Vector implements Random Access .

What is mean by Random Access ?

Random access interface helps in retrieving the elements allover the list with the same speed.Ie If retrieving the first element takes 1 second then same time is required to retrieve the kth element also. So Time taken is very less .


No comments:

Post a Comment