refermycode.com

Develop your world..

Need of Collections framework in Java

In this tutorial, we will going to discuss about the Need of Collections framework in Java. As we have alternate ways to store the data in Java then why we should use the Collection framework?

To answer this question, let us study the some points which is given below:

In traditional way, if we required multiple variables in our java program to store the data then we can use multiple variable by declaring it with its datatypes but in this scenario if numbers of variables are increased then it will decrease the readability of our program due to declaration of multiple variable in our program.

To overcome this problem, later we can use arrays, to store the multiple values in single variable. But using array include several problems which is explained below,

  1. When we want to use array in our program, we need to define the size of array in declaration of array. This will restrict the user to add element with limited size of array, means We can not add element greater than the size which is mentioned while declaration of array. If user still trying to add element to array then it will gives the array out of bound exception.

Collection1

  1. If user declared an array with maximum number of elements and at execution time if our program use lesser numbers of element then it will cause the wastage of memory. Hence we can not shrink or grow the size of an array dynamically, we should need to know exact size of an array in advance.
  1. We can insert the element with the same type (homogeneous datatype) which we have declared while array declaration, means we can not add element with heterogeneous datatype. If we will going to add element with heterogeneous datatype then this will throws incompatible type execution while compilation of java program.

Collection2

  1. There is no any predefined method are available for array due to that, we have to write our own logic to perform some basic operation like searching and sorting.

To overcome above mentioned problems, we need to use Collections in our programming language.

Advantage of Collections:

  1. Collections can be Growable in nature, means we can add or remove elements without defining predefined fixed size.
  2. Collections can hold heterogeneous elements as well as homogeneous elements, we can study this chapter in generous chapter.
  3. Collections are based on some data structure which provides some predefined method to perform basic operation like searching and sorting.

Difference between Array and Collections :

ArrayCollections
1. Arrays are Fixed in size we can not add element dynamically.1. Collection are Growable in nature, we can add elements dynamically.
2. With respect to memory array are not recommended to use.2. With respect to memory Collections are recommended to use.
3. With respect to performance array are recommended to use.3. With respect to performance Collections are not recommended to use.
4. In Array we can insert only Homogeneous data.4. In Collections we can insert Heterogeneous as well as Homogeneous data also.
5. Underlying data structure not available hence readymade methods are not available.5. Every Collection is based on some data structure.
6. We can insert primitive elements as well as object.6. We can insert only objects but not primitive elements.

Post a comment