- Java, Java-Collections

Why We Need Collection Framework in Java?

A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task. Before Collection Framework(or before JDK 1.2) was introduced, the standard methods for grouping Java objects (or collections) were Arrays or Vectors, or Hash tables. All of these collections had no common interface. Therefore, though the main aim of all the collections is the same, the implementation of all these collections was defined independently and had no correlation among them. And also, it is very difficult for the users to remember all the different methods, syntax, and constructors present in every collection class.Collection Framework is a powerful framework in java. This framework defines the most common methods that can be used for any collection of objects. But the question arises that we have an array concept in java then why we need collection framework in java? Now let’s see that why we need collection framework in java with some valid points of difference between array and collection.Syntax: Declaring variablesint x = 10 or int y = 30We declare the variables in our program as shown above which are initialized to custom random integer values. But like this how many elements we are going to declare? What if I want to declare 100 and 1000 elements in the code then the single variable declaration method is not suitable for declaration. Here array concept came into the picture. For the declaration of 1000 variables or elements, we can declare an array with some size. Array concept is very efficient and suitable for various operations. Syntax:Student[] s = new Student[5];Array is very efficient for some operations But there are some limitations while we are using arrays like:Arrays are fixed in size i.e. once we created an array with some size then there is no increasing or decreasing its size based on the requirements.Arrays can hold only homogeneous data elements.Array concept is not implemented based on some standard data structure. Hence ready-made methods are not available for the requirement.  Illustration: Student s = new Student[1000];
we can declare like this : s[0] = new Student
but we cannot declare like this : s[1] = new customerTo overcome these drawbacks or limitations of the array we need collection framework in java. Collection framework is used with various operations and having various in-build methods. They are as follows:As collection framework is growable in nature some need not worry about the size.Collection framework can hold both homogeneous and heterogeneous objects.Collection framework is implemented based on some standard data structure. Hence, ready-made methods are available to use as per the requirement.  Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.