TechVeerendra’s Software Solutions The Collections Framework How can we handle a group of elements? We can use an array to store a group of elements and handle them easily. How can we handle a group of Objects? We can use an array to store a group of objects. Yes, it is possible to use an array to store a group of objects. Using an array to store a Group of Objects: Employee.java public class Employee { int id; String name; public Employee(int id, String name) { this.id = id; this.name = name; } public void display() { System.out.println(id + ": " + name); } } TechVeerendra’s Software Solutions EmployeeGroup.java import java.util.Scanner; public class EmployeeGroup { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Employee arr[] = new Employee[5]; for(int i=0; i<arr.length; i++) { System.out.println("Employee id: "); int id = sc.nextInt(); System.out.println("Employee name: "); String name = sc.next(); arr[i] = new Employee(id, name); } for (int i = 0; i < arr.length; i++) { arr[i].display(); } } } An Array is an Indexed Collection of Fixed Number of Homogeneous Data Elements. The Main Advantage of Arrays is we can Represent Multiple Values by using Single Variable so that Readability of the Code will be Improved. Limitations of Object Type Arrays: Arrays are Fixed in Size that is Once we created an Array there is no Chance of Increasing OR Decreasing Size based on Our Requirement. Hence to Use Arrays Concept Compulsory we should Know the Size in Advance which may not Possible Always. Arrays can Hold Only Homogeneous Data Type Elements TechVeerendra’s Software Solutions Test.java Student[] s = new Student[10]; s[0] = new Student(); s[1] = new Student(); s[3] = new Customer(); We can resolve this problem by using Object type array Oject[] o = new Oject[10]; o[1] = new Student(); o[2] = new Customer(); Adding the Objects at the end of an array is easy. But inserting and deleting the elements in the middle of the array is difficult. In this case we have to re-arrange all the elements of the array. Retrieving the elements from an array is easy but after retrieving the elements if we want to process them, then there are no methods available to carry out this. There is no underlying data structure for array. Hence readymade data structure support we can’t expect. For every requirement we have to write code explicitly which increases complexity of the programming. Due to these problems, programmer want a better mechanism to store a group of objects. The alternative is using an object to store a group of objects. It means we can use a class object as an array. such a class object is called collection object or container object. TechVeerendra’s Software Solutions Collection Objects: A collection object or container object is an object which can store a group of other objects. A collection object has a class called collection class or container class. All the collection classes are available in the package of java.util A group of collection classes is called a collection framework. Collections are Growable in Nature. That is based on our requirement we can Increase or Decrease the Size. Collections can Hold Both Homogeneous and Heterogeneous Elements. Every Collection Class is implemented based on Some Standard Data Structure. Hence for Every Requirement Readymade Method Support is Available. Being a Programmer we have to Use those Methods and we are Not Responsible to Provide Implementation. Differences Between Arrays and Collections: Array Collections Arrays are Fixed in Size. Collections are Growable in Nature. With Respect to Memory Arrays Are Not With Respect to Memory Collections are Recommended to Use. Recommended to Use With Respect to Performance Arrays are With Respect to Performance Collections are Recommended to Use. Not Recommended to Use Arrays can Hold Only Homogeneous Data Collections can Hold Both Homogeneous Elements and Heterogeneous Elements Arrays can Hold Both Primitives and Collections can Hold Only Objects but Not Objects. Primitives. Arrays Concept is Not implemented based on For every Collection class underlying Data Some Standard Data Structure. Hence Readymade Available. Method Support is Structure is Available Hence Readymade Not Method Support is Available for Every Requirement.