Collections Framework in Java

Collections framework in Java is one of the most valuable and interesting topics of Java language. How important it is?

Without using collection concepts, you cannot develop any production level software application in Java. Therefore, every Java learner has to learn the collections framework.

According to the Sun Microsystem document, among all the java concepts, Java collections framework is the most important concept for developing the project and crack interviews also.

An interviewer often asks questions to check whether the interviewees understand the correct usage of collection classes and are aware of available alternative solutions.

This is because each Java collection implementation is created and optimized for a specific type of requirement.

In this Java collections framework tutorial, we will cover the following topics in depth.

  • What is Collection in Java?
  • Realtime examples of Collection
  • Types of objects stored in Collection (container) object
  • What is the need for Collections in Java?
  • What is Collections Framework in Java?
  • Difference between Array and Collection in Java and many more.

Initially, collection framework is simply called Java.util package or Collection API. Later on, Sun Microsystem had 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 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. Let’s understand it with some realtime examples.

Real-time Examples:Realtime example of collections framework 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 a 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 or container which stores a group of other objects as a single unit or single entity. Therefore, it is also known as container object or collection object in java.

A container object means it contains other objects. In simple words, a collection is a container that stores multiple elements together.

JVM (Java Virtual Machine) stores the reference of other objects into a collection object. It never stores physical 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.

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 collections framework in java.

A simple example of a collection is given below:

// Create a container list of cities (objects or elements). 
   List<String> city = new ArrayList<String>();

// Adding names of cities.
   city.add(“New York”);

Types of Objects Stored in Collection (Container) 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");

A unique or duplicate object depends on its internal data.

What is need for Collections in Java?

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

1. Using 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 this:

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.
  • The readability and reusability of the code will be down.
  • JVM will take more time for execution.

Hence, the problem facing this approach can be overcome by using the second approach “Class object”.

2. Using 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. Look at the code.

class Employee {
   int eNo;
   String eName;
// Creating an object of Employee class.
   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 the third technique “Array object”.

3. Using 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. an array can store only similar types of data. Suppose if we create an Employee type array object like this:

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.

From the above code, it is clear that we cannot store different class objects into the same array. This is because an array can store only one data type of elements (objects).

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, we must know the size of an array to store a group of objects in advance, which may not always be possible.

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 collection object:

By using collection object, we can store the same or different data without any size limitation. Thus, technically, we can define the collections 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 Collections Framework in Java?

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

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.

In other words, a collections framework is a class library to handle groups of objects. It is present in java.util package. It allows us to store, retrieve, and update a group of objects.

Collections framework in Java supports two types of containers:

  • One for storing a collection of elements (objects), that is simply called a collection.
  • The other, for storing key/value pairs, which is called a map.

Java collections framework provides an API to work with data structures such as lists, trees, sets, and maps. It offers a set of reusable data structures, algorithms, and utilities for managing collections of objects in Java programming.

It saves developers time and effort by providing efficient implementations of common data structures. In multi-threaded environments, working with collections can be challenging because of the possibility of race conditions and other concurrency issues.

Key Interfaces in Collections Framework

Java programming language built the collections framework around four core interfaces:

  • Collection
  • List
  • Set
  • Map

These interfaces provide different ways of managing groups of objects, depending on the requirements of your application. The list of interfaces defined in java.util package is shown in the below table:

List of Interfaces defined in java.util package


Some of the most commonly used implementations of the Collections Framework in Java are as:

  • ArrayList
  • LinkedList
  • HashSet
  • HashMap
  • TreeMap
  • TreeSet

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

List of classes defined in java.util package


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.

Advantage of Collections 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.

Limitation of Collections Framework in Java

There are two limitations of the collections framework in Java. They are as follows:

  1. Care must be taken to use the appropriate cast operation.
  2. Compile type checking is not possible.

Q. Does a collection object store copies of other objects?

A: No, a collection object works with reference types. It stores references of other objects, not copies of other objects.

Q. Can we store a primitive data type into a collection?

A: No, collections store only objects.

In this tutorial, you have learned about collections framework in Java with realtime examples. We have covered almost all the topics under collections framework in easy words.

Hope that you will have understood basic definition of collections in Java and enjoyed this topic. In the next tutorial, we will discuss the collection interface and hierarchy in Java. If you like this tutorial, please share your love by sharing it on the social networking sites and help your friends.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love