What is Collection Framework in Java? | Hierarchy & Interfaces of Java Collection Framework | Java Collections Methods & Interfaces

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?

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.

Interfaces of Collections FrameWork

The following image represents the interfaces of collection framework in java:

java collections framework interfaces

Need 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.

  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 only homogenous types of elements.
  3. 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.

  1. Collections are growable in nature, depends on our requirements we can grow and shrink the size of the array.
  2. Collections can hold homogenous along with 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.

Advantages of the Collection Framework

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:

  1. Consistent API
  2. Increases program speed and quality
  3. Reduces programming effort

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:

What is Collection Framework in Java 1

What is Collection Framework in Java 2

Java Collection Interface

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.

Methods of Collection interface

No. Method Description
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.

Subinterfaces of the Collection Interface

Here is the list of Subinterfaces of the Collection Interface:

List 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.

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 orders are not preserved. HashSet, LinkedHashSet, and TreeSet are the implementation classes of the Set interface.

SortedSet 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.

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 the Map interface.

SortedMap 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.