Collections in java

In this article, we are going to discuss Collections in Java uses. What are the advantages of collection in java? Why we need Collections in java and where we use it.

 

Java collection

The need for Collection:

Array: An Array is an indexed collection of a fixed number of homogeneous data elements.

Using arrays the main advantage is we can represent multiple values in a single variable by using arrays. So that Readability of the Code will be Improved.

 

There are some limitations of object type arrays:

  1. Arrays are fixed in size that means if Once we created an array there is no chance of increasing or decreasing size based on our requirement. So when we are using arrays it is compulsory we should know the size of arrays in advances which may not be possible every time.                                                                                                                 
  2.  Arrays can hold only homogeneous data type elements.                                                                                   Example:

    Student[] s = new Student[10000];
    s[0] = new Student(); // Working beacuse we are creating Student object. Student[] s = new Student[10000];
    s[0] = new Customer()); //We are getting error CE: incompatible types found: Customer required: Student                       
  3. Arrays Concept is Not implemented based on some standard data structure, hence readymade methods support is not available. Hence for every requirement, we have to write the code explicitly which increases the complexity of the programming. 

To Overcome this type of arrays problems we should go for Collection.

 

What are the main advantages of collection:

  1. The collection is growable in nature. That means we can change the size(Increase OR Decrease) based on Our Requirement.
  2. It can hold both homogeneous and Heterogeneous Elements.
  3. Every collection Class is implemented based on some standard data structure. Hence for every requirement readymade method support is available. Being a programmers we have to use those methods and we are not responsible to provide the implementation.

 

Collection:

When we want to Represent a Group of Individual Objects as a Single Entity then we should go for Collection.

Differences Between Arrays And Collection:

Arrays:

  1. Arrays are fixed in size.
  2. With respect to memory, arrays are not recommended to use.
  3. With respect to performance, arrays are recommended to use.
  4. Arrays can hold only homogeneous data elements.
  5. Arrays can hold both Primitives and Objects.
  6. Arrays concept is not implemented based on some standard data structure. Hence readymade method support is not available.

Collection:

  1. The collection is growable in nature.
  2. With respect to memory, collections are recommended to use.
  3. With respect to performance, collections are not recommended to use.
  4. Collection can hold both Homogeneous and Heterogeneous elements.
  5. Collection can hold only objects but not primitives.
  6. For every collection class, the underlying data structure is available hence readymade method support is available for every requirement.

Collection Frame Work:

It defines several Classes and Interfaces which can be used to represent a group of Objects as
a single entity.

 

There are mainly 9  Key Interfaces Of the Collection Framework.

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

Your email address will not be published.