Why Collection Framework ?

The Collection Framework in Java is a collection of interfaces and classes to store, process and transfer the data efficiently. 

Why do we need Collection Framework ?

In Java we can store and transfer the data in the following ways.

  1. Primitive data types
  2. Primitive Arrays
  3. User Defined Objects
  4. User Defined Object Array
  5. Object Type Object Array
  6. Collection Framework

Primitive data types

We can use primitive data types for storing only one element data and only one type of data. For Example,

  • Here I can store only one employeeId value
  • I can’t store multiple employeeIds
  • I can’t store any other type of data other than int
  • I can’t use this to transfer many values from one method to another

NOTE: We can transfer the data from one method to another method as a method parameter or method return value.

If we want to store many employeeId values then we could go for primitive array.

Primitive Arrays

We can use primitive arrays for storing many values of same data type. For Example,

  • Array is fixed in size
    • We can’t store more than the size specified
    • If we store fewer items than the specified size, memory will be gone waste
  • We can’t store heterogeneous type of elements
  • We can’t transfer multiple types of data from one method to another method

User Defined Objects

Here ‘user defined object’ can store multiple data elements of multiple types. For Example,

  • We can’t transfer group of Employee’s data from one method to another with one ‘Employee’ instance

User Defined Object Array

Here each ‘user defined object’ can store multiple data elements of multiple types. Then ‘user defined object array’ can be used to store multiple objects. For Exampl.e

  • array is fixed in size
    • We can’t store more than the size specified
    • If we store fewer items than the specified size, memory will be gone waste
  • We can’t store any other type other than ‘Employee’

java.lang.Object Array

Here we can store heterogeneous or homogenous Objects. For Example,

  • array is fixed in size
    • We can’t store more than the size specified
    • If we store fewer items than the specified size, memory will be gone waste
  • For processing(traverse, add, delete, modify …etc) data in array there is no predefined method support

Thus if our requirement is store and process multiple objects then we go for Collection Framework.

Collection Framework

  • The Collection Framework in Java is a collection of interfaces and classes to store, process and transfer the data efficiently. 
  • Collections are growable in nature. i.e. based on run time requirement we can store any number of elements.
  • Collections can hold both homogeneous and heterogeneous data elements.
  • We can transfer the data from one method to another of any type and any number of elements.
  • For every requirement ready mate method support is available. Hence being a programmer we just have to know how to use the predefined methods.

Hence if our requirement is representing group individual objects as a single entity then the better option is ‘Collection framework’.

Arrays vs Collections

ArrayCollection
Fixed in sizeGrowable
Can hold only homogeneous data Can hold both homogeneous & heterogeneous data
No predefined method supportFor every requirement methods are available
Arrays can hold both primitives and objectsCollections can hold only objects
Scroll to Top