In this Blog we are going to see what is collection framework in java , why is it needed and how to use it.
If you are reading this then probably you have made up your mind to use Java. Ok, but now the question arises what is Collection and why do we need it?.
A collection is an object that represents a group of objects. Let’s admit it that as a programmer we need data structure and not one but a variety as per our needs. Not only do we need structures to hold our data but we also need algorithms and methods to manipulate them. That’s when Collection Framework comes into the picture. It is an architecture with a set of classes and interfaces which lets us in representing and maintaining the collections.
Before the Collection Framework Java had classes like Dictionary, Vector, Stack, Properties to store and manipulate a group of objects. Although there were quite useful they lacked a central, unifying theme. Therefore Collection Framework was designed to meet several goals like high performance, allow different collections to work similarly and with high interoperability, and extending or adapt a collection should be easy.
There are many Advantages of Using the Collection Framework like reduces programming effort, increases performance, provides interoperability between unrelated APIs, reduces the effort to design and implement API, and enhances software reusability.
Collection Framework Heirarchy
The Collection Framework consists of Collection Interface, Algorithms, Array Utilities, implementations like partial implementation, general-purpose implementation, legacy implementation, etc, some map interfaces. Although maps are part of the collection framework they are not “collection” in strict terms.
Before looking at different components of the Collection Framework let’s first understand what is a class and an interface.
Class : class is a user-defined template consisting of properties and methods using which we create an object.
Interface : Similar to a class interface can have variables and methods but methods in an interface are abstract; meaning they do not have a body. Interfaces are used to tell a class what they should do ad not how.
It is the root interface of the entire collection framework. Collection Framework extends the iterable interface hence all classes and interfaces inherently implement it. The Java Iterable interface represents a collection of objects which is iterable — meaning which can be iterated. We can do this in 3 ways.
- Using a Iterator
- Using a for-each loop
- Using forEach() method
This interface extends the iterable interface and is the foundation on which Collection Framework is built.
All the classes that define the Collection framework implement the Collection Interface. Collection declares the core methods which will be implemented by all the collections irrespective of the style of implementation.
The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. It can contain duplicate elements. Classes ArrayList, Vector, LinkedList, and Stack implements the List Interface.
ArrayList provides us with dynamic arrays. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array can be shrunk. Array list cannot have a primitive data type, for such cases, we have to use wrapper class.
LinkedList uses a doubly-linked-list to store the elements and is an implementation of the linked-list data structure. It maintains the insertion order and can contain duplicate elements. It implements List, Dequeue, and Queue interfaces. Elements here are not stored in contiguous locations and each element is a separate object with its data and address part.
Vector is like a dynamic array that can grow or shrink its size. It implements the List interface and is found in java.util package. It s similar to the ArrayList and the main difference between them is that while ArrayList is non synchronized vector is synchronized.
Stack class is the implementation of the Stack data structure. It is based on the last-in-first-out principle. Along with the basic operations like push and pop it also has methods like peek, search, and empty.
The Set interface defines a set. It extends Collection and specifies the behavior of a collection that does not allow duplicate elements. This collection is used if we want to only add unique elements. The add method returns false if we try to add duplicate elements to the set.
It inherits the AbstractSet class and implements the Set interface. It uses a hash table for storage. It stores objects based on their hashcode therefore it does not guarantee to maintain the insertion order. It allows the insertion of null elements.
It inherits the HashSet class and implements the Set interface. It uses a doubly-linked-list to store the data. it is non-synchronized. It is similar to the HashSet but maintains the insertion order.
The Queue interface extends Collection and declares the behavior of a queue, which is a first-in-first-out list. It is used in scenarios where the ordering matters. For example in booking a ticket the person who comes early in the queue books the ticket first.
- Priority Queue
It inherits the AbstractQueue class. It is used when objects are to be sorted based on priority. It does not order the element in a FIFO manner. It is based on the priority heap.
The Deque interface extends Queue and declares the behavior of a double-ended queue. Double-ended queues can function as standard, first-in, first-out queues or as last-in-first-out stacks. It is a linear collection that supports insertion and deletion at both ends.
It implements the Dequeue interface and inherits the AbstractCollection class. It provides us with the facility of a re-sizeable array. It does not have capacity restrictions and can grow as per the needs.
The SortedSet interface extends Set and declares the behavior of a set sorted in ascending order. The only difference with the Set interface is that it has extra methods to maintain the ordering of elements.
The TreeSet uses a Tree for storage. The objects of TreeSet are stored in ascending order. It is non-synchronized. Access and retrieval are fast.
The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. It does not allow duplicate keys but we can have duplicate values.
It implements the Map interface and provides a basic implementation of a map to store a key and value pair. If we try to duplicate a key it will replace the current value and store a new value.
It inherits the HashMap class and implements the Map Interface. It only stores unique keys. It can have one null key and many null values. It is non-synchronized and maintains the insertion order.
The SortedMap interface extends Map. It ensures that the entries are maintained in ascending order based on the keys. It extends the Map interface and provides the total ordering of the elements.
It implements the SortedMap interface and extends the AbstractMap class. it uses a red-black-tree to store the elements. It is non-synchronized. It cannot have a null key but have multiple null values. It maintains the ascending order.
References- Java The Complete Reference