Introduction

Before the Java 2 version, Java used to render some ad hoc classes, such as Array, Dictionary, Stack, and Vector, for manipulating groups of objects in the programming. Even though these classes were useful, they lacked a universal theme. All the earlier mentioned classes were different from one another. That is where the Collection Framework in Java came into existence. In this article, you will learn about the Java Collection Framework in-depth and programs on the Collection Framework in Java. 

In this Article. Let us look into:

  1. What Is A Framework?
  2. What is Collection Framework in Java?
  3. Hierarchy of Collection Framework in Java
  4. Various Methods in the Collection Framework in Java
  5. Interfaces in Java Collection Framework
  6. Collection Framework in Java with an Example
  7. Advantages of Collection Framework

1. What Is A Framework?

Before we explain a Collection Framework in Java, it is essential to understand what a framework is. A Framework is a platform or a body with pre-written codes having a collection of classes and interfaces that present a ready-made architecture for implementing those elements. For implementing a new feature, programmers do not need to define a framework.

2. What is Collection Framework in Java?

Collection Framework in Java is a collection of individual objects, forming a unified architecture for storing and manipulating objects within a program. JDK 1.2 defines a separate framework known as the ‘Collection Framework’ that holds the collection classes and interfaces within it. It includes all other collection classes as a part of itself. The Collection Framework has two root interfaces. These are the Collection interface (java.util.Collection) and the Map interface (java.util.Map). The framework collection in Java can accomplish all the operations that a programmer needs to perform on data, such as insertion, searching, sorting, manipulation, and deletion. 

Now, let us now understand the Java Collection Framework in detail in this article. 

3. Hierarchy of Collection Framework in Java

At the top level of the Collection Framework, there are various interfaces. These interfaces comprise numerous methods for performing distinct operations on collection objects. All the interfaces and classes in the Collection Framework in Java reside within the utility package (java.util). Here is the Collection Framework hierarchy diagram in Java.

 Here, in the Java Collection Framework, classes are used as a user-defined blueprint or prototype from which programmers create objects. It represents methods and properties that are universal to all objects of the same type. Interfaces in the Java Collection Framework are like a class blueprint. They are classes containing methods and variables. But these methods and variables declared in an interface are abstract; that is, they have the method signature without a method body. Interfaces know what a class can do but do not know how to do it.

4. Various Methods in the Collection Framework in Java

Methods are a block of codes that will execute only when they get called. There are various methods declared in the Collection Interface. They are as follows:

  Serial No.    Method Names  Method Description
1add( E e )Programmers use this method for inserting an element in the collection. 
2addAll (Collection<? extends E> c)Programmers use this method for inserting specific collection elements into the invoked collection. 
3remove (Object element)Programmers use this method for deleting an element from within the collection. 
4removeAll (Collection<?> c)Programmers use this method for deleting all the elements belonging to the specified collection. 
5retainAll(Collection<?> c)Programmers use this method for deleting all the elements belonging to the invoked collection except for the particular collection. 
6int size()This method returns the total number of elements of the specified collection. 
7void clear()This method removes all the number of elements from the collection. 
8contains(Object element)This method searches a particular element. 
9containsAll(Collection<?> c)This method searches the specified collection from various collections. 
10add(Object)Programmers use this method for adding an object to the collection. 
11addAll(Collection c)Programmers use this method for adding all the elements in the given collection. 
12contains(Object o)Programmers use this method to return true if the collection contains the specified element. 
13removeIf(Predicate filter)Programmers use this method for removing all the elements of the collection, satisfying the specified predicate. 
14equals(Object o)Programmers use this method for comparing the specified object among this collection for equality. 
15hashCode()This method returns the value of hash code for this collection. 
16isEmpty()This method returns true only when this collection does not contain any elements. 
17iterator()This method will return an iterator on the elements in this collection. 
18spliterator()This method creates a Spliterator on the elements within this collection. 
19stream()This method returns a successive stream with the collection as the source. 

5. Interfaces in Java Collection Framework

Multiple interfaces in the Collection Framework extend the collection interface. Here we have mentioned the interfaces present within the Collection Framework in Java with example programs.

  • Iterable Interface: The entire Collection Framework resides within this root interface. The collection interface extends this interface. Multiple interfaces in the Collection Framework extend the collection interface. Here all the other interfaces exist within the Collection Framework, and all other classes and interfaces implement this interface inherently.

Example: Iterator iterator();

  • Collection Interface: The collection interface extends the iterable interface, and all other collection classes and interfaces reside within this. It has all the declaration of the methods that other collections will implement.
  • List Interface: It is an ordered collection that allows adding and removing elements as well as permit duplicate data to reside within it. The list interface can implement ArrayList, Vector, Stack, Properties, etc. Example:

List <T> Alist = new ArrayList<> ();

List <T> vec = new Vector<> ();

List <T> LL = new LinkedList<> ();

where ‘T’ defines the type of the object

  • Set Interface: It is an unordered collection of objects that can store elements in different sets like that of a mathematics set. It does not allow storing duplicate values and allow storing unique values only. The set interface can implement TreeSet, HashSet, LinkedHashSet, etc.

Example:

Set <T> hs = new HashSet<> ();

Set <T> lhs = new LinkedHashSet<> ();

Set <T> tset = new TreeSet<> ();

where ‘T’ defines the type of the object.

  • Queue Interface: It is another interface of the Collection Framework used to store and access elements maintaining the FIFO (First In First Out) order. It is a dedicated interface used for storing objects where the ordering of elements matters. The queue interface uses various classes like Deque, PriorityQueue, ArrayDeque, etc.

Example:

Queue <T> pque = new PriorityQueue <> ();

Queue <T> adq = new ArrayDeque <> ();

where ‘T’ defines the type of the object.

  • Deque Interface: Its working has a small variation from that of the queue. The double-ended queue is a data structure in which we can add and remove elements from both ends. It extends the queue interface and implements the ArrayDeque.

Example:

Deque<T> adq = new ArrayDeque <> ();

where ‘T’ defines the type of the object.

  • Map Interface: This interface supports the map data structure containing key-value pairs for mapping the data. It does not hold duplicate keys. Maps are useful when programmers wish to perform operations based on the mapped keys.

Example:

Map<T> hm = new HashMap<> ();

Map<T> tm = new TreeMap<> ();

where ‘T’ defines the type of the object.

Out of all the mentioned classes, Stack, HashTable, Properties, Dictionary, and Vector are legacy classes in Collection Framework in Java. 

Now, let us take a look at the Collection Framework in Java with an example program.

6. Collection Framework in Java with an Example

Here is a real-time example of a Collection Framework in Java. It showcases how to use the Collection Framework in Java along with its various classes and interfaces such as ArrayList, LinkedList, Map, and Set within a program.

import java.util.*;

public class CollectionFrameworkEg {

   public static void main(String[] args) {

   //ArrayList in Java Collection Framework

   List al = new ArrayList();

      al.add(“Karlos”);

   al.add(“Ray”);

   al.add(“Gaurav”);

   System.out.println(“Collection of ArrayList Elements:”);

   System.out.print( “\t” + al);

   System.out.println();

   //Linked List in Java Collection Framework

   List Llist = new LinkedList();

      Llist.add(“Dee”);

   Llist.add(“Bill”);

   Llist.add(“Steve”);

   System.out.println();

   System.out.println(“Collection of LinkedList Elements:”);

   System.out.print(“\t” + Llist);

   //HashMap in Java Collection Framework

   Map hm = new HashMap();

   hm.put(“Java”, “7”);

   hm.put(“Python”, “2”);

   hm.put(“C”, “3”);

   hm.put(“JavaScript”, “1”);

   System.out.println();

   System.out.println(“Collection of Mapped elements:”);

   System.out.print(“\t” + hm);

   System.out.println();

   //HashSet in Java Collection Framework

   Set hs = new HashSet();

   hs.add(“Ray”);

   hs.add(“Karlos”);

   hs.add(“Gaurav”);

   System.out.println(“Collection of Set elements:”);

   System.out.print(“\t” + hs);

   }

}

7. Advantages of Collection Framework

As discussed earlier, before Java 2, all the earlier mentioned classes were different from one another. It is one of the reasons why we use a Collection Framework in Java. Apart from that, it has numerous advantages.

  1.  With the advent of Collection Framework, programmers now do not have to learn to integrate multiple APIs to build a program.
  2. Using the Collection Framework, programmers can write high-performance implementations through useful data structures and their methods and increase the code performance.
  3. The standard interface of the Collection Framework not only fosters software reusability but also reduces programming effort so that programmers do not need to write them all by themselves.
  4. The collection in Java is resizable and flexible to program.

Conclusion:

The Collection Framework has become a boon to the programmers due to the sophisticated hierarchical design of classes and interfaces. It has various methods within its subclasses that make the Java program flexible using different data structures. There are multiple data structures such as ArrayLists, sets, hashmap, queue, deque, etc., and their methods have made programming effortless. Code reusability is possible because of the different features of the Collection Framework. Its advent makes operations such as searching, insertion, sorting, manipulation, deletion, etc., easy for Java programmers. If you are interested in learning more about software development, don’t forget to check out the Master Certificate In Full Stack Development offered by Jigsaw Academy.

Also, Read

Access Modifiers in Java: An Easy 3 Step Beginner’s Guide

Top 5 Features of Java 13: Every Programmer Must Know

SHARE
share

Are you ready to build your own career?