Java Collection Framework is a combination of 2 words ie, Collection and Framework. A Collection is a group of objects represented as a single unit and a Framework is a set of classes and interfaces. Want to learn more about the Collection Framework in Java? Then, this tutorial can be your best choice. Here, we have discussed fully on java collection framework right from the definition, need, hierarchy, interfaces, etc.
- What is Collection Framework in Java?
- Interfaces of Collections FrameWork
- Need of Collection Framework in Java
- Advantages of the Collection Framework
- Hierarchy of Collection Framework
- Java Collection Interface
- Methods of Collection interface
- Subinterfaces of the Collection Interface
Collection framework hierarchy: A collections framework is a unified architecture for representing and manipulating collections. The Collection Framework gives a well-designed set of classes and interfaces for storing and manipulating a group of data as a single unit in Java.
We can implement all the operations on data like searching, sorting, insertion, manipulation, and deletion using the Java Collection Framework. Before we explain more details about the Java Collection Framework, let’s get a clear understanding of the Java Collection Framework Needy.
Hierarchy of collection framework: The following image represents the interfaces of collection framework in java:
Before the Collection Framework (before JDK 1.2) was entered, the standard methods for grouping java objects were Arrays. So first, learn the problems with Arrays.
- Arrays have a fixed size. Once we declared the size of an array, we can’t grow and shrink the size of the array
- Arrays can hold only homogenous types of elements.
- To perform operations such as sorting, searching, insertion, and deletion on an array you have to define methods.
To defeat the problems of arrays, we should go for Collection Framework in Java.
- Collections are growable in nature, depends on our requirements we can grow and shrink the size of the array.
- Collections can hold homogenous along with heterogeneous elements.
- 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.
As the lack of a collection framework furnished growth to the above set of disadvantages, the listed points are the advantages of the collection framework:
- Consistent API
- Increases program speed and quality
- Reduces programming effort
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:
The collection interface is recognized as a root interface of the collection framework which is performed by all the classes in the collection framework. The collection interface determines most of the common methods that every collection will have. If we require to describe a group of objects as a single unit then we should go for the collection interface.
|1||public boolean add(E e)||It is used to insert an element in this collection.|
|2||public boolean addAll(Collection<? extends E> c)||It is used to insert the specified collection elements in the invoking collection.|
|3||public boolean remove(Object element)||It is used to delete an element from the collection.|
|4||public boolean removeAll(Collection<?> c)||It is used to delete all the elements of the specified collection from the invoking collection.|
|5||default boolean removeIf(Predicate<? super E> filter)||It is used to delete all the elements of the collection that satisfy the specified predicate.|
|6||public boolean retainAll(Collection<?> c)||It is used to delete all the elements of invoking collection except the specified collection.|
|7||public int size()||It returns the total number of elements in the collection.|
|8||public void clear()||It removes the total number of elements from the collection.|
|9||public boolean contains(Object element)||It is used to search an element.|
|10||public boolean containsAll(Collection<?> c)||It is used to search the specified collection in the collection.|
|11||public Iterator iterator()||It returns an iterator.|
|12||public Object toArray()||It converts collection into array.|
|13||public <T> T toArray(T a)||It converts collection into array. Here, the runtime type of the returned array is that of the specified array.|
|14||public boolean isEmpty()||It checks if collection is empty.|
|15||default Stream<E> parallelStream()||It returns a possibly parallel Stream with the collection as its source.|
|16||default Stream<E> stream()||It returns a sequential Stream with the collection as its source.|
|17||default Spliterator<E> spliterator()||It generates a Spliterator over the specified elements in the collection.|
|18||public boolean equals(Object element)||It matches two collections.|
|19||public int hashCode()||It returns the hash code number of the collection.|
Here is the list of Subinterfaces of the Collection Interface:
The List interface extends the collection interface. A list may contain duplicates values. In the List interface insertion order must be preserved. ArrayList, LinkedList, Vector, Stack is the implementation classes of the List 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.
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.
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 orders are not preserved. HashSet, LinkedHashSet, and TreeSet are the implementation classes of the Set interface.
The SortedSet interface is the child interface of the Set interface. It does not contain duplicates elements. This interface declares the behavior of a set sorted in ascending order.
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 the Map interface.
The SortedMap interface extends the 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.