Need of Collection Framework in Java

Share

When we think about a small piece of data, we always use a variable to store it. Let’s go to a step further, what if we need to store large dataset,  for that purpose, we also have the concept of Array. We can precisely say, an array is used to hold large dataset of the same type having random access capability.

But the downside of the array approach is that it limits the flexibility of handling the data, one of them is the size of the array which cannot be changed at runtime.  It means we cannot expand or shrink the size of an array at the time of program execution, as a result, the probability of memory wastage or insufficient memory allocation can occur. So, we should know the size in advance to confront the size issue. The second disadvantage is that it can only store homogeneous dataset, which is again a problem. This demerit raises the issue of the inability to hold different types of data.

And the last one, the array is not based on some specific standardized data structure, such as Stack, List, this is why there are no inbuilt methods for sorting, searching and more. For each functionality, one requires to write the entire code i.e sorting, searching and so on.

To overcome these problems we use collections which sees a collection of objects as a single entity.

These advantages  are:

  1. The size can be expanded or shrink automatically
  2. It can hold both heterogeneous and homogeneous data. We don’t need to know the size in advance, instead,  it allocates memory dynamically
  3. It is based on standardized data structure and therefore you get inbuilt methods

This post was last modified on June 20, 2020

Sandeep Verma

Published by
Sandeep Verma
Tags: advantages array collections disadvantages java