Collections framework in Java is one of the most valuable and interesting topic of Java language. How important it is? Without using the collection concept, you cannot develop any project. Every Java learner has to learn the collection framework.

According to Sun Microsystem document, among all the java concepts, the collection framework is the most important concept for developing the project and to crack the interview also because this topic is the most favorite topic for the interviewer. So let us understand java collection framework in this tutorial in depth.

Initially, collection framework is simply called Java.util package or Collection API. Later on, The Sun Microsystem has introduced the collection framework in Java 1.2. It was developed and designed by "Joshua Bloch". 

Later on, after Java 1.2, it is known as the collections framework. From Java 1.5 onwards, The Sun Microsystem added some more new concepts called Generics. Due to adding Generics concepts, a lot of changes happened in the collections classes. That's why Java 1.5 onwards, people started calling it Collections and Generics.

What is Collection in Java


A collection is a group of objects. In Java, these objects are called elements of the collection.

Real-time Examples:

Realtime examples of collection in java
1. In childhood, you had a kiddy bank. In the kiddy bank, you had collected a lot of coins. This kiddy bank is called collection and the coins are nothing but objects.

2. During school time, you put all the important books in the school bag before going to school. Here Schoolbag is a collection and books are objects.

3. A classroom is a collection of students. So, the classroom is nothing but a collection and students are objects.

4. We know that the entire world is the collection of humans, animals, and different things. The world is a collection and humans, animals and different things are different objects.

Technically, A collection is an object which stores a group of other objects as a single unit or single entity. Therefore, it is also known as container object in java. Container object mean it contains other objects. 

JVM stores the reference of other objects into a collection object. It never stores copies of other objects because other objects are already available in the memory and storing another copy of objects into a collection object would be a wasting of memory.

A group of objects stored in a collection object is shown in the below figure. In the figure, we are storing 5 objects in a collection object. 
A group of objects stored in a collection object.
A collection object has a class that is known as collection class or container class. All collection classes are present in java.util package. Here, util stands for utility. A group of collection classes is called collection framework in java.

Types of Objects Stored in Collection Object


There are two types of objects that can be stored in a collection or container object. They are as follows:

1. Homogeneous objects:

Homo means same. Homogeneous objects are a group of multiple objects that belong to the same class.
For example:
Suppose we have created three objects Student s1, Student s2, and Student s3 of the same class 'Student'. Since these three objects belong to the same class that's why they are called homogeneous objects.

2. Heterogeneous objects:

Hetero means different. Heterogeneous objects are a group of different objects that belong to different classes.
For example:
Suppose we have created two different objects of different classes such as one object Student s1 and another one object Employee e1. Here, student and employee objects together are called a collection of heterogeneous objects.

These objects can also be further divided into two types. They are as follows:

1. Duplicate objects:

The multiple objects of a class that contains the same data are called duplicate objects.
For example:
Suppose we create two person objects Person p1 and Person p2. Both of these objects have the same data. 
     Person p1=new Person( "abc");
     Person p2=new Person("abc");

Since the above two objects have the same data "abc" therefore, these are called duplicate objects.

2. Unique objects:

The multiple objects of a class that contains different data are called unique objects.
For example
        Person p1=new Person("abcd");
        Person p2=new Person("abcde");
Unique or duplicate object depends on its internal data.

What is need of Collection in Java?


Before going to understand the need for collection in java, first, we understand different ways to store values in Java application by JVM. There is total of four ways to store values in Java application by JVM.

1. Using a variable approach: 

Suppose you want to store one value in the memory then you will create one variable and assign a value like this
            int x=10;
The purpose of variable x is to store one int value 10. If we want to store two int values, we will create two variables like 
           int x=10;
           int y=20;
Similarly, for three values, the third variable will be required and so on.

There is no problem to store until the third or fourth value. But if we want to store 5000 values then declaring 5000 variables in a program is the worst kind of programming practice and it will not be in the readable format.

Thus, the limitations of using the variable approach are as follows:
➲ The limitation of a variable is that it can store only one value at a time. 
➲ Readability and reusability of the code will be down.
➲ JVM will take more time for the execution.
Hence, the problem facing with this approach can be overcome by using the second approach "Class object".

2. Using a class object approach: 

Using a class object, we can store multiple "fixed" number of values of different types.
For example, suppose we have created a class named Employee and declare two variables inside the class.
      class Employee {
        int eNo;
        String eName;
   } 
If we create an object of Employee class like
       Employee e1=new Employee(); 
So can you think how many values can store in this employee object?

The answer is only two but if you will want to store the third value, it will not possible. Therefore, this approach is only suitable to store a fixed number of different values. To overcome this problem, we should use third technique "Array object".

3. Using an array object approach:

We can store a group of objects into an array. Let's take an example to understand it.
Suppose we want to store 5000 objects of Student class into an array. For this purpose, we need to create an array object of Student type like this:
           Student[ ] st=new Student[5000];
This array can store 5000 Student objects.
Array representation to store a group of objects in java
The biggest advantage of an array is that we can store a huge number of values by using a single variable st and retrieve them easily. Array mechanism helps to improve the readability of the code in java programming but there are several types of problems and limitations with the array. They are as follows:

1. We can easily store multiple "fixed" numbers of values of homogeneous data type i.e. Array can store only similar types of data.

Suppose if we create Employee type array object
         Employee[ ] emp=new Employee[5000]; // it will hold only employee type objects.
For example:
          emp[0]=new Employee(); // valid.
          emp[1]=new Customer(); // invalid because here, we are providing the customer type object. 
We can resolve this problem by using an object array.
     Object[ ] ob=new Object[5000];
     ob[0]=new Employee(); // valid.
     ob[1]=new Customer(); // valid.

2. An array is static in nature. It is fixed in length and size. We cannot change (increase/decrease) the size of the array based on our requirements once they created. Hence, to use an array, it is compulsory that we should know the size of an array to store a group of objects in advance which may not be possible always.

3. We can add elements at the end of an array easily. But, adding and deleting elements or objects in the middle of array is difficult.

4. We cannot insert elements in some sorting order using array concept because array does not support any method. We will have to write the sorting code for this but in the case of collection, ready-made method support is available for sorting using Tree set.

5. We cannot search a particular element using an array, whether the particular element is present or not in the array index. For this, we will have to write the searching code using array but in the case of collection, one readymade method called contains() method is available.

Due to all these above limitations of array, programmers need a better mechanism to store a group of objects. So, the alternative option is a collection object or container object in java.

4. Using a collection object:

By using a collection object, we can store the same or different data without any size limitation. Thus, technically, we can define the collection as:

A collection in java is a container object that is used for storing multiple homogeneous and heterogeneous, duplicate and unique elements without any size limitation.

What is Framework in Java?


A framework in java is a set of several classes and interfaces which provide a ready-made architecture.

What is Collections Framework in Java


A Java collections Framework is a sophisticated hierarchy of several predefined interfaces and implementation classes that can be used to handle a group of objects as a single entity. It is present in java.util package. It allows us to store, retrieve, and update a group of objects. 

The java collections framework provides an API to work with data structure such as lists, trees, sets, and maps. The list of interfaces defined in java.util package is shown in the below table:

List of Interfaces defined in java.util package

Collection List Queue
Comparator ListIterator RandomAccess
Deque Map Set
Enumeration Map.Entry SortedMap
EventListener NavigableMap SortedSet
Formattable NavigableSet
Iterator Observer

The list of important classes present in java.util package is shown in the below table:

List of classes defined in java.util package

AbstractCollection EventObject Random
AbstractList FormattableFlags ResourceBundle
AbstractMap Formatter Scanner
AbstractQueue AbstractSequentialList HashMap
AbstractSet HashSet Stack
ArrayDeque Hashtable StringTokenizer
ArrayList LinkedList Vector
Collections EnumMap EnumSet
Calender LinkedHashMap TreeMap

Difference between Arrays & Collections in Java


The difference between arrays and collections are as follows:
1. Arrays are fixed in size but collections are growable in nature. We can increase or decrease size.

2. Arrays are not recommended to use with respect to memory whereas collections are recommended to use with respect to memory.

3. Arrays are recommended to use with respect to performance but collections are not recommended to use with respect to performance.

4. Arrays can store only homogeneous data elements (similar type of data) but collections can hold both homogeneous and heterogeneous elements.

5. Arrays do not support any method but collections support various kinds of methods.
6. Arrays can have both hold primitives and object types but collections can hold only objects but not primitive.

Advantages of Collection Framework in Java


The advantages of the collections framework in java are as follows:
1. The collections framework reduces the development time and the burden of designers, programmers, and users.

2. Your code is easier to maintain because it provides useful data structure and interfaces which reduce programming efforts.

3. The size of the container is growable in nature.
4. It implements high-performance of useful data structures and algorithms that increase the performance.
5. It enables software reuse.

Limitations of Collection Framework in Java


There are two limitations to java collection framework. They are as follows:
1. Care must be taken to use the appropriate cast operation.
2. Compile type checking is not possible.

Recommended tutorials on Java Collections Framework:
Final words
Hope that this tutorial has covered almost all important points related to the basic of collections framework in java with realtime examples. I hope that you will have understood collection in java and enjoyed this topic. In the next tutorial, we will discuss the collection interface and hierarchy in java.
Thanks for reading!
Next ➤ Collection Hierarchy in Java ⏪ Prev Next ⏩