What is Collection?
It is a group of individual object as a single entity.
- It provides readymade architecture.
- It represents a set of classes and interfaces.
- It is optional.
for eg:-
As we know that collection are arrays somehow, same but sometime why we don't use arrays why use collection than, let discussed about that why these are different from each other?What is Array?
An array is an indexed collection of Fixed Number of Homogeneous data element.
What is the Main advantage of Array?
.we can represent multiple values by using single variable so, Readability of the code will be improved.
What is the Limitations of Arrays?
1.Arrays are fixed in size. Once we create an array there is no chance of increasing or decreasing the size based on our requirement, due to this to use array concept compulsory we should know the size in advance which may not possible.
2.Array can hold only Homogeneous data type elements.
Suppose, I have took Student Array
Student[]s=new Student[10];
Here, only Student type of object we can hold.
s[0]=new Student(); Valid
s[1]=new Customer(); Not Valid
But now we have question than How to resolve this problem?
We have solve this problem only by using Array.
.Any type of Object hold by requirement
.Go for Object Array
object[]a=new object[12];
a[0]=new Student(); Valid
a[1]=new Customer(); Valid
3.Arrays concept not implemented based on some standard data structure hence, Readymade method support is not available for every requirement, we have o write code explicitly which increase complexity of programming.
To overcome these problem we should go for Collection concept.
The Main Advantage of Collection
1.Collection are Growable in nature[Not Fixed in Size] that is based on our requirement, we can increase or decrease the Size.
2.Collection can hold both Homogeneous and Heterogeneous element.
3.Every Collection class implemented based on some standard data structure hence, Readymade method support is available for every requirement, being a programmer we are responsible to use those methods.
What is the difference between Array & Collection?