Pages

Monday, June 1, 2020

Collection in Java


What is collection:

1. When we want to represent group of an entity into single entity then we should go for collection.

2. It allows us to achieve sorting order, insertion order, remove duplicates, operations based FIFO & LIFO, searching, manipulations, fast retrieving of objects/data, add or remove of objects/data.

3. It mainly consist of List, Set and Queue interfaces which allows us to perform many operations and provides ready made methods for several operations.

4. All the classes and interfaces in "java.util" belongs to Collections
Collection hierarchy
Fig: Collection hierarchy


5. Above figure shows Collection hierarchy which consist of many interfaces and classes.


Que: Why collections came into picture instead of Array?

Ans: From below difference between Array and Collection we can see why collection came in java:


Array
Collection
1
Fixed in size
Growable in nature
2
Memory point of view array are not preferred.
Highly recommend as per memory point of view.
3
Recommend for good performance.
Not recommended as per performance.
4
Can hold only homogeneous datatype element.
Can hold both homogeneous heterogeneous datatype.
5
Can hold both objects and primitives.
Can hold only objects.
6
No any underlying data structure.
Underlying data structure for collection.
7
NO any readymade method support.
Support for readymade method.


Note: We'll see List, Set and Queue in separate sections.

No comments:

Post a Comment

**** Please let me know if you have any doubts. ****