What is Collection Framework in Java?

Collection Framework in Java is a combination of 2 words Collection and Framework. A Collection is a group of objects represented as a single unit and Framework is a set of classes and interfaces. The Collection Framework provides a well-designed set of classes and interfaces for storing and manipulating a group of data as a single unit. We can perform all the operations on data such as searching, sorting, insertion, manipulation, and deletion using the Java Collection Framework. Before discussing the Java Collection Framework in details let's first understand the need of the collection framework.

Need of Collection Framework in Java:

Before Collection Framework (before JDK 1.2) was introduced, the common methods for grouping java objects were Arrays. Let's first understand the problems with Arrays.
1. Arrays have a fixed size. Once we declared the size of an array, we can't grow and shrink the size of the array
2. Arrays can hold the only homogenous type of elements.
3. To perform operations such as sorting, searching, insertion, and deletion on an array you have to define methods.
To overcome the problems of arrays we should go for Collection Framework in Java.

1. Collections are growable in nature, based on our requirements we can grow and shrink the size of the array.
2. Collections can hold homogenous as well as heterogeneous elements.
3. In collections, predefined methods are available for every requirement. Being a programmer we have to use this predefined method and we are not responsible to provide the implementation.

Hierarchy of Collection Framework

All the classes and interfaces for the collection framework are available in java. util package. Let's see the hierarchy of collection framework given below:

Collection Framework in Java

Collection Framework in Java

Collection Interface: 

Collection interface is considered as a root interface of the collection framework which is implemented by all the classes in the collection framework. Collection interface defines most of the common methods that every collection will have. if we want to represent a group of objects as a single unit then we should go for the collection interface.

List Interface:

The List interface extends the collection interface. A list may contain duplicates values. In List interface insertion order must be preserved. ArrayList, LinkedList, VectorStack is the implementation classes of List interface.

Queue Interface:

The Queue interface extends Collection and declares the behavior of queue, which is often a first-in, first-out order. If we want to represent a group of individual objects prior to processing then we should go for the Queue interface.

Deque Interface: 

The Deque interface was added by Java SE6. It extends the Queue interface and declares the behavior of a double-ended queue. In Deque we can insert and delete the elements from both the side. Deque enables us to perform the operations at both ends.

Set Interface:

The Set interface defines a set. It extends the Collection interface and declares the behavior of a collection that does not allow duplicate elements. In Set interface insertion order are not preserved. HashSet, LinkedHashSet, and TreeSet is the implementation classes of the Set interface.

SortedSet Interface:

The SortedSet interface is the child interface of Set interface. It does not contain duplicates elements. This interface declares the behavior of a set sorted in ascending order.

Map Interface:

The Map interface maps a unique key to values. A key is an object that you use to retrieve a value at a later code. Key and values both are objects. HashMap, LinkedHashMap, TreeMap, Hashtable are the implementation classes of Map interface.

SortedMap Interface: 

The SortedMap interface extends Map interface. It ensures that the values are maintained in ascending order based on the keys. Both Key and value are objects, duplicates key is not allowed but values can be duplicated. 

You may also like:

What is Serialization in Java?
Exception Handling in Java
Constructor, Types of Constructor, and Constructor chaining in Java 
What are Java Beans?
Inheritance, Types of Inheritance in Java
What is J2EE?

What is Collection Framework in Java? What is Collection Framework in Java? Reviewed by Prashant Srivastava on February 11, 2019 Rating: 5

1 comment:

  1. Nice article. Thanks for sharing.



Powered by Blogger.