Collections in Java
In the above blog, we learned about Assertions in Java. If you want to know more about Assertions visit Assertions in Java . In this blog, we will see how the traditional data structuring required for series programming is accomplished by the Java Library. In our college, we used to have a subject called Data Structures and which required a whole semester to go through the important concepts present in that subject. In this blog, we will not go through the theory but we will see how to use collections classes in the standard library.
Collection Framework
Before starting with Collection Framework, let's first understand the term Collection and Framework:
Collection - A collection is a group of individual objects which are represented as a single unit.
Framework - A framework is nothing but the platform which provides a base(foundation) for developing applications. Consider it as a template of a code or a program wherein we can add or modify the code as per our needs and coming to the Collection Framework, the Collection framework is nothing but the framework which provides an architecture to manipulate and store a group of objects. Java Collection includes the following concepts:
- Interfaces
- Classes
- Algorithms
The interface is just like a class that can have methods and variables, the only difference is that in the interface the methods are declared as abstract by default. In short, it is a blueprint of a class. Interfaces play a vital role in Collection Framework. java.util.Collection is a root interface of the Collection Framework and it is on the top of the Collection framework hierarchy. There are other interfaces too which are also very important i.e. java.util.List, java.util.Set, java.util.Queue and java.util.Map. java.util.Map interface is exceptional as it is not inherited from the Collection interface, but still, it is part of the Collection Framework.
A class is a template or design(blueprint) from which the objects are made. When you construct an object from a class, then it creates an instance of the class. In Java Framework CLasses are used for creating different types of collections in the program. ArrayList, LinkedList, HashMap, TreeMap, HashSet, and TreeSet are collection classes that are used by the developers to fulfill the programming needs. The collection classes are present in the java.util and java.util.concurrent packages.
Algorithm refers to the methods which are utilized to carry out operations such as searching, sorting, etc.
Need of Collection Framework
Before Collection Framework was introduced, the standard methods for collections were Array, Hashtables, and Vector. There was no common interface for these Collections. Though the goal of all collections is the same, the implementation is entirely independent and has no correlation between them. Also from the user perspective, it is very difficult for them to remember all the syntax, methods, etc in every collection class. The other drawback is that the Vector contains all the methods that are final i.e. we cannot extend the ’Vector’ class to implement the same kind of Collection. In order to overcome all the above issues, Java developers came up with a framework called Collection Framework in JDK 1.2.
Advantages of Java Collection Framework
The advantages of Java Collection Framework are:
- Reduced coding efforts
- Increased code quality and speed of a program
- Reusability and Interoperability
- Reduced efforts required for learning, designing a new API
Collection Framework provides all common types of collections and methods which are useful for iterating and manipulating data. As the collection framework provides all the common collections and methods, we can focus on the business logic rather than focusing on designing our own collection API.
As we are using core collection classes that are well-tested, improves the quality of a code and speed rather than using any home-developed data structure.