Skip to content

What is the need of Collection Framework in JAVA?

  • by

Why need of Collection in java? In simple words, the Java Collections Framework allows you to use the right data structure because one size does not fit all. Java Collections is come up with different functionality, convenience, and usability.

We have two approaches:

  1. Individual variable approach:
  2. Array

Individual variable approach:

  • Suppose we want to create or store one int value in the memory. Like int x=10, here x is representing INT type value. Now suppose I want to store one more value I will create another variable int y=10; So on and so forth I can create a variable but If suppose I need to allocate 1000 variable or values. We can create 1000 variables but it’s not a good programming practice or it will not be readable.
  • So, Declaring 1000 variables is the worst kind of programming practice.
  • The readability and reusability of the code will be down because which variable represents which value is difficult to tell and remember.

TO OVERCOME ALL THSESE PROBLEMS WE SHOULD GO FOR ARRAYS CONCEPT.

Arrays:

An array is an indexed collection of fixed number of homogeneous data elements.

Main Advantage:

  • In an array, we can represent multiple values with a single variable.
  • The reusability of the code will be improved.

Limitations of object type arrays:

  • Arrays are fixed in size: Once we created an array with some size there is no chance to increase and decrease its size based on our requirement, Hence to use arrays compulsory we should know the size in advance which may or may not possible.
  • Arrays can hold only homogeneous data elements.

For ex:

Student[] s = new Student;   (Correct)

S[0] = new Customer;    (Wrong)

Compiler Exception: Incompatible type

Found: Customer

Required: Student

But we can resolve this problem by using object arrays.

Object[] o = new Object[1000];

o[0] = new Student[];

o[1] = new Customer[];

Arrays Concept is not implemented based on some standard data structure hence ready-made method support is not available for every requirement. We have to write the code explicitly which is the complexity of programming.

Individual Variable Approach

 

Huge number of variable

Int x=10;

Int y-10;

Int z=10;

Array

 

Student [] S = new Student[1000];

000      

0           1          2         3 ————————-    999

S

We can represent huge no. of variable by using a single variable S

  • Array concept is not implemented using any datastructure.

Suppose we want to insert an element in some sorting order then who will respond to write the sorting code programmer

But in collection readymade method support is available for sorting we can use Tree Set (Automatically element will insert in some sorting order)

  • Second, If we want to search whether the particular element is present or not. So in arrays we will have to write the searching codes.

But in collection we have one readymade method called contains()

About Arrays:

1. Fixed in size.

2. Homogenous elements

3. Underlying DS

4. Contains()

TO OVERCOME ALL THESE PROBLEMS WE SHOULD GO FOR COLLECTIONS CONCEPT

Read Next http://tutorial.eyehunts.com/java/collections-framework/

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading