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.
- Primitive data types
- Primitive Arrays
- User Defined Objects
- User Defined Object Array
- Object Type Object Array
- 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,
1 |
int employeeId; |
- 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,
1 2 |
int employeeIds[] = new int[10]; // Here in 'employeeIds' array we can store multiple values of 'int' type |
- 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,
1 2 3 4 5 6 7 8 9 |
public class Employee implements Cloneable { private int employeeId; private String name; private double salary; // Setters & Getters } // In 'Employee' class objects we can store multiple types(int, String, double) of data // In 'Employee' class objects we can store multiple data elements(employeeId, name, salary) |
- 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
1 2 3 4 |
Employee[] employees= new Employee[10]; // Each 'Employee object' can store multiple data elements of multiple types. // Employee array can store multiple employees data // We can transfer multiple employees from one method to another |
- 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,
1 2 3 |
java.lang.Object[] objects = new java.lang.Object[10]; // Here can store any objects like 'Employee' or 'Student' or 'Customer' // We can transfer multiple objects from one method to another |
- 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
Array | Collection |
---|---|
Fixed in size | Growable |
Can hold only homogeneous data | Can hold both homogeneous & heterogeneous data |
No predefined method support | For every requirement methods are available |
Arrays can hold both primitives and objects | Collections can hold only objects |