Why do we need a collection framework in Java?

An Array is a collection of the same type of elements in a contiguous memory allocation. These are used to represent lists of things like numbers, strings etc.


<element-type>[] <array-name> = new <element-type>[<array-size>];


To implement array follow these steps

  • Step 1 − Selecting an appropriate data type suitable for each individual element that will populate your desired array should be carefully considered beforehand.

  • Step 2 − Furthermore, determining the necessary capacity required by taking into account specific usage requirements will enable selection of an accurate and optimal array size.

  • Step 3 − Declare an array variable.

  • Step 4 − To access the various elements of the array. Simply use the index operator [] and perform any necessary operations.

  • Step 5 − To perform relevant actions on each and every element contained within an array. One can employ loops as a way of going through them systematically and executing the required tasks.


public class Main {
   public static void main(String[] args) {
      int[] marks = new int[3];
      marks[0] = 75;
      marks[1] = 20;
      marks[2] = 87;
      for (int i = 0; i < marks.length; i++) {
         System.out.println("Marks of student " + (i + 1) + ": " + marks[i]);
      int sum = 0;
      for (int i = 0; i < marks.length; i++) {
         sum += marks[i];
      double average = (double) sum / marks.length;
      System.out.println("Average marks: " + average);


Marks of student 1: 75
Marks of student 2: 20
Marks of student 3: 87
Average marks: 60.66

Limitations of Array in java

  • Arrays are fixed in size − Size of array cannot be increased or decreased during run time.

  • Arrays are not memory efficient − If the number of elements added to an array is less than the size that has been allocated, there is a chance that memory will be wasted.

  • No built-in methods available in Array − Array do not have any built-in method to perform common operations like adding, searching etc..

  • Arrays hold only homogeneous data elements − Arrays can store elements of the same type only.

  • No underlying data structure − The idea of an array is not implemented using a standard data structure. As a result, there is no ready-made method support available.

Collection framework in java

In Java, a framework is designed to provide a standardized way or we can say it provides a ready-made architecture to solve a particular problem or task by utilizing a set of abstract classes and interfaces, and other components.

A collection is a group of individual objects as a single entity. Several different classes and interfaces are available within Java's collection framework which provide effective ways to represent collections. Popular selections such as ArrayList, LinkedList, HashSet. And TreeSet can be accessed through packages in java.util.


The syntax depend on the specific class for example −


ArrayList<T> list = new ArrayList<T>();


LinkedList<T> list = new LinkedList<T>();


To implement follow these steps −

  • Step 1 − Choose the appropriate collection class based on the requirements of your program.

  • Step 2 − Import the necessary classes for the collection.

  • Step 3 − Declare a variable of the collection class.

  • Step 4 − Instantiate the collection using the appropriate constructor.

  • Step 5 − Use the method as per the requirement.

  • Step 6 − Use the collection as needed in your program.


import java.util.ArrayList;
public class Main {
   public static void main(String[] args) {
      ArrayList<String> names = new ArrayList<String>();
      System.out.println("Names: " + names);
      names.set(1, "Neha");
      System.out.println("Names: " + names);
      for (String name : names) {
         System.out.println("Name: " + name);


Names: [Apoorva, Anamika, Supriya]
Names: [Apoorva, Neha, Supriya]
Name: Apoorva
Name: Neha
Name: Supriya

Hence, the array's drawbacks or limitations can be overcome using the Java collection framework. Therefore we need a collection framework. This framework’s advantages are as follows −

  • Growable nature of collection − Now size is not a problem in collections because of its growable nature we can increase or decrease the size at run time.

  • Collections are memory efficient − elements can be increased or decreased based on the requirements therefore collections are recommended with respect to memory point of view.

  • Built-in methods are available in collections − Collections have many built-in methods to perform common operations like adding, searching etc..

  • Collection holds homogenous and heterogenous data elements − Collection can hold the same as well as different types of element.

  • Standard Data Structure − Collections are based on standard data structure therefore every collection support for ready made methods is available.


It is evident that both arrays and collections exhibit distinctive advantages and disadvantages. Your programs' specific requirements are pivotal in deciding between them. In cases where data size is predetermined and quick access to elements is necessary, arrays prove to be more suitable. Collections are better suited for situations where you need more flexibility in the structure of the data and want built-in methods for manipulating it.

Updated on: 01-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started