Know Your Collections! — Part 1, Introduction to Java Collections.
What are collections ? Why are they useful ? Why is it necessary to know about them?
These may be the questions you have right now and you will get a clear answer to all these questions in a little while. Our department’s Carriers Fair was held in the recent days and, a very common question almost all the interviewers asked from me was about Java Collections. So after facing those interviews, I thought of writing this article to give a brief introduction about Collections myself.
So, as the name implies, Java Collections are used to store a group of objects in a certain data structure. Just like Python’s lists and JavaSciprt’s arrays, Java has its own method to store a group of data in a program and that is called a Collection.
Those Collections provide additional functionality apart from storing a set of elements. Adding new elements, removing elements, iterating through them, and looking for a certain element can be named as some of those functionalities.
How is a Collection Defined in Java
So you may wonder, how these “Collections” are defined in Java? The answer to that is the Collection Interface. Java has a specific interface called Collection<E>. If any class/interface implements this interface that class/interface is technically a Collection. The Collection interface specifies the basic methods that a collection should have like add(), clear(), contains(), etc.
In Java, there is a Collection hierarchy. That means, there are sub-interfaces that implement this Collection class and there are subclasses of them and so on. So all these make a tree of Collections with Collection<E> interface as the root element. Each of the sub-interfaces of Collection has its own specific features and methods. The basic java collections hierarchy is given below.
So as you can see the Collection interface is divided into 3 interfaces, Set, List, and Queue, which have specific properties of their own.
Set Interface
In mathematics “Set” is defined as a collection of well defined and distinct objects. Here, distinct means that no element can exist twice in a Set i.e. elements should be unique. To model this concept in Java, Set<E> Interface is introduced.
A Collection that can have no duplicate elements is known as a set. It inherits the basic Collection methods which were defined previously in the root interface and it adds this functionality which is unique to the Set interface only.
Queue Interface
If a certain data structure follows the First-In-First-Out (FIFO) strategy when dealing with adding and removing elements, it can be named as a queue. Queue interface in java is added to implement this concept in java. Apart from the basic methods which were provided by the Collection interface, Queue<E> interface provides some additional methods for inserting and removing data. It offers poll(), remove() methods to get an element which is at the head of the queue and remove that element from it and add(), offer() methods to add a certain element to the tail of the queue.
List Interface
Last but not least, there is the List Interface. A List
is an ordered collection of elements in Java where several methods are provided to access those elements. This is the most used collection out of all three major sub interfaces of the Collection<E>
interface. Unlike in Sets, this interface allows duplicate elements in them. List interface provides additional methods for accessing and searching its elements index-based.
As you can see the 3 main sub-interfaces of the Collection interface have properties of their own and can be used in different unique use cases. As you go along in your Java-Programming journey you will see how each of these interfaces is used on separate occasions.
Concrete Implementation of Interfaces
As you already know, even though Java has specified these sweet interfaces for data management. We cannot use them directly! We gotta have some implementation of those interfaces.
For that purpose, Java has introduced a ton of different classes and interfaces which are subclasses of these main 3 interfaces. Those classes and interfaces address different unique requirements and a large Collections hierarchy is made from those. Below is a glimpse of the large number of different collections that Java offers for very specific scenarios.
So I think by now you must have a clear basic understanding of the structure of the Collections in Java and how the Collection hierarchy is built. I will conclude my article here and in the next article, I will explain the Set Interface and its subclasses in detail. If you have any questions please do not hesitate to contact me. Cheers! :)