Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Collections Framework

The Framework is a readymade structure containing several classes & interfaces that can be used to solve problem in a specific way.
Similarly, Collections framework in Java consists of set of classes & interfaces.
This framework is present in the java.util package.
Collections framework helps us to use the group of objects as a single body. That is collections framework works with group of single objects as shown below:

Java Programming Language Collections Framework

Collections framework consists of interfaces like List, Set, Queue, etc. & classes like ArrayList, Vector, Hashset, PriorityQueue, etc.

Need of Collection Framework

Before launching Collection Framework, Java was using several special purpose classes such as Dictionary, Properties, Stack & Vector.
These classes lacked in many important & useful features. Collection Framework provided solution for those lacking features.

Some important features of collection

i) Collections are resizable

Collections can grow & shrink dynamically (at runtime), to match need of memory space.
Results in proper use of memory space to avoid memory wastage.

ii) Collections can contain homogeneous as well as heterogeneous objects

Collections can be a group of similar or different objects.
Without Generics, the collection can contain heterogeneous (different type) objects.
With Generics, the collection can contain only homogeneous (same type) objects.

Java Programming Language Collections Framework 2

iii) Collections can hold only object elements & not primitive types of elements.

Impact of Generics on Collection Framework

Before generic, Collection Framework was using Object type reference to refer objects. So, it could store any type of objects. But this may result in mismatch errors at runtime which is extremely bad.
Generics solved this problem by providing type safety.
Generics also solved the problem of type casting.
If you do not understand what we are talking about, you need to learn Generics by clicking here.

Collection Framework Hierarchy
Java Programming Language Collection Framework Hierarchy
Benefits of Collections Framework

If we want to store heterogeneous (different type) elements we can use collections without generics.
If we want to store only homogeneous (same type) elements, it is possible in collections with the help of generics.
It is possible to create dynamic arrays, that is the size of the array can increase & decrease at runtime.
Pre-defined methods are available.

Now let’s study each interface & class of collections framework in detail.

 

The Collection Interface

Declaration

interface Collection<E>

The Collection interface is the main interface of all the Collections Framework hierarchy.
It extends the Iterable interface. The Iterable interface allowed us to use for-each loop in Collections Framework.

Note

We have tried to represent programs in two forms, using Generic & Non-Generic.
For Generic programs we have used Generic interfaces, classes, constructors & methods.
For Non-Generic programs we have used Non-Generic interfaces, constructors & methods.
Few examples of methods in both Generic & Non-Generic form are given below.

Java Programming Language Collection Interface Methods

All Methods of Collection Interface (CLICK HERE)

 

Leave a reply