Why do we need collections in Java

Before using any collections in java we should have knowledge that why do we need collections. Suppose, If collections were not there in Java  and we have to store some data.

1) Individual variable approach.

In this approach, let suppose we have some Integer data , so following this approach we can store data into variables.

But what will happen, If we have large amount of Integer data, suppose some thousands of integer data, so it will be awful to store data into variables, instead of that we can store data into array.

 

2) Use of Array to store data.

An array is indexed collection of fixed no of homogeneous data elements.  The main advantage of Array is , we can represent multiple values by using single variable, so that readability of the code will be improved.

But there are some limitation involved with Arrays  as collection.

(i) Arrays are fixed in size that is once we create an array, there is no chance of increasing or decreasing the size based on our requirement, due to this to use Arrays concept, compulsory we should the size in advance, which may not possible always.

(ii) Arrays can hold only homogeneous data type elements.  As we know Array provide Type safe collection. Let see this issue by an example.

We can solve this heterogeneous problem by using  Object array itself, see in below code

 

(iii) Arrays concept is not implemented based on some standard data structure, and hence ready made method support is not available.  For every requirement, we have to write code explicitly, which increases complexity of programming. To overcome above problems of Array, we should go for Collections concept.

  Use of Collections

So instead of using Array, if we use Collections, we can solve above problem related to Array.

(i) As Array are fixed in size, Collections are grow-able in nature, based on our requirement, we can increase or decrease the size.

(ii) Instead of using Array which support only homogeneous , Collections can hold both homogeneous & heterogeneous elements.

(iii) Array don’t have any underlying data structure. Every Collection class is implemented based on some standard data structure, hence ready-made method support is available for every requirement. Being a programmer we have to use this method and we are not responsible to provide implementation.

Difference between Arrays and Collections

     Arrays Collections
With respect to memory Arrays are not recommend to use. For Example, if we declared array of 1000 elements, but at run time allocate memory for 5 elements , remaining 995 waste.

 

With respect to memory recommended to use.
 Fixed in size  Grow-able in nature i.e based on our requirements we can increase or decrease the size.
 With respect to performance recommended to use.  With respect to performance not recommended.
 Can hold only Homogeneous elements.   Can hold both Homogeneous & Heterogeneous elements.
 There is no underlying data structure and hence ready-made method support is not available. Every Collections class is implemented based on some data structure. Hence ready-made supports available for every requirements.
 Array can hold both Primitive and Object types.

int[] a;

Integer[] b;

 

 Collections can hold only Objects but not primitives.

 

Note) If you know the size in advance – highly recommended to go for Arrays only.

Leave a Reply

Your email address will not be published. Required fields are marked *