Java Collections Framework
In this article, I am going to discuss the Java Collections Framework in detail. Please read our previous article where we discussed Multi-Dimensional Arrays in Java with Examples. Collections concept introduced in Java 1.2 version to resolve the problems of the array. As part of this article, we are going to discuss the following pointers in detail which are related to the Java Collections Framework.
- What is an Array and its Limitation in Java?
- Definition and need of Java Collections Framework
- What are Collections?
- What is a Framework?
- Advantages of Java Collection Framework
- Collection Interfaces and its Hierarchy in Java
- Methods of Collection Interface in Java
- What is the need for collection framework classes?
- Is collection classes are java data structures?
What is an Array and its Limitation in Java?
An array is a collection of elements that are started in contiguous memory locations.
Limitations of Array:
- The size of an array is fixed, which means once array size is decided it can’t be increased or decreased. Whit this sometimes memory may be wasted or sometimes memory may not be sufficient.
- There are no predefined functions to perform operations like insertion, deletion, searching, sorting, etc. So that as a programmer we have to write our own logic.
- Array stores only a group of similar elements because of the problems in an array, Java comes up with a Collection Framework.
Definition and need of Java Collections Framework:
A collection is a java object that is used to group homogeneous and heterogeneous and duplicate and unique objects without size limitation for carrying multiple objects at a time from one application to another application among multiple layers of MVC architecture as method arguments and return type.
What are Collections?
Collections: It is a mechanism of collecting some group of objects either statically or dynamically. Collections can hold a group of objects. The size of the collection is not fixed, it means when we are inserting and deleting the elements than the size of the collection dynamically increased or decreased. Every collection internally follows data structures and contains predefined functions it means we don.t need to write new logic for performing the operations like Insertion, Deletion, Searching, Sorting, etc.
What is a Framework?
The framework is a semi-finished reusable application that provides some common low-level services for serving reoccurring problems and that can be customized according to our requirements. For example:
- The computer is a framework; it can be used by all people according to their requirements.
- Switchboard is a framework it can be used by all people according to their requirement.
- In java struts, hibernate, spring technologies are frameworks, all these technologies are used by different companies for developing projects according to the project requirements.
From Java Point of View, Framework means a collection/set of well-defined classes and interfaces that provides ready-made services support to our application. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task.
An object is said to be a collection object if it holds or stores a group of other objects. The collection never stores any primitive values, but if we want to store primitive values we have to represent primitive values as objects.
Collection class is a class whose object can store a group of other objects. Example: ArrayList, HashSet, HashMap, etc. All the collection classes are available in the “java.util” (utility) package. All the collection interfaces and collection classes together form a Collection Framework.
Java.util package contains several classes that collect homogeneous and heterogeneous objects without size limitation. These classes are usually called collection framework classes. All the collection classes are classified into the following four categories:
- List: It is used to store a group of individual elements where the elements can be duplicated.
- Set: It is used to store a group of individual elements but the elements can’t be duplicated.
- Queue: It is used to hold multiple elements prior to processing and is dedicated to storing all the elements where the order of the elements matter.
- Map: It is used to store the element in the form of key-value pairs where the keys can’t be duplicated but values can be duplicated.
Why these package classes are called Collection Framework?
This package class provides low-level services with well-defined data structures to solve collecting a heterogeneous dynamic number of objects as a single entity. Due to this reason java.util package classes are collectively called the collection framework.
All collection classes are implementing from java.io.Serializable so the collection object along with all its internal objects can be stored in a local file system or can be sent across the network to the remote computer. Here the rule is objects stored in the collection are also should be Serializable types to store collections in the file.
Collection stores a group of objects as follows:
Here objects o1, o2, o3, and o4 are stored two times thereby we are wasting the memory within the JVM. To save the memory within the JVM when the objects are stored in the collection object, the JVM stores the references of the objects within the collection objects instead of storing the copy of objects directly.
Advantages of Java Collection Framework:
- It is used as a temporary data storage (avoid hitting the database for getting the same amount of data.
- It reduces the complexity of the application (i.e. array code is reduced). That means Reduces programming effort by providing useful data structures and algorithms so we don’t have to write them by our self.
- Increases performance by providing the high-performance implementation of useful data structures and algorithms because the various implementation of each interface are interchangeable, programs can be easily tuned by switching implementations.
- We do not have to write code to implement these data structures and algorithms manually.
- Our code will be much more efficient as the collections framework is highly optimized.
The Collection interface is the root interface of the collections framework hierarchy. Java does not provide direct implementations of the Collection interface but provides implementations of its sub-interfaces like List, Set, and Queue.
Collection Interface Hierarchy in Java
Collection Interface has the following three sub-interfaces:
- List Interface
- Set Interface
- Queue Interface
Methods of Collection Interface in Java
This interface contains various methods that can be directly used by all the collections which implement this interface. They are:
- add(Object): This method is used to add an object to the collection.
- addAll(Collection c): This method adds all the elements in the given collection to this collection.
- clear(): This method removes all of the elements from this collection.
- contains(Object o): This method returns true if the collection contains the specified element.
- containsAll(Collection c): This method returns true if all the collection contains all of the elements in the given collection.
- equals(Object o): This method compares the specified object with this collection for equality.
- hashCode(): This method is used to return the hash code value for this collection.
- isEmplty() : this method return true if this collection contains no elements.
- iterator(): This method returns an iterator over the elements in this collection.
- max(): This method is used to return the maximum value present in the collection.
- parallelStream(): This method returns a parallel Stream with this collection as its source.
- remove(Object o): This method is used to remove the given object from the collection. If there are duplicate values, then this method removes the first occurrence of the object.
- removeAll(Collection c): This method is used to remove all the objects mentioned in the given collection from the collection.
- removeIf(Predicate filter): This method is used to removes all the elements of this collection that satisfy the given predicates.
- retainAll(Collection c): This method is used to retails only the elements in this collection that are contained in the specified collection.
- size(): This method is used to return the number of elements in the collection.
- spliterator(): This method is used to create a Spliterator over the elements in this collection.
- stream(): This method is used to return a sequential Stream with this collection as its source.
- toArray(): This method is used to return an array containing all of the elements in this collection.
What is the need for collection framework classes?
In java projects, collection framework classes are used to store and transport objects of the same and different types without size limitation.
Project code design with collection framework:
- As we know every project has three layers Model, View, and Controller. The model layer application collects data from DB using ResultSet and stores it in the collection object and returns this collection object to the controller layer application.
- Then the controller layer application reads data from the collection object and fills it in view layer required code using HTML components.
- Finally, this HTML code is passed to the client browser displays the result to the end-user.
Is collection classes are java data structures?
Basically, collection framework classes are java data structures. These classes internally use several standard data structures to collect objects such as an array, vector, stack, Queue. Linked list. Hash table, tree, etc. so collection object size is automatically incremented and decremented
In the next article, I am going to discuss List collections in Java with examples. Here, in this article, I try to give an overview of the Java Collections Framework and I hope you enjoy this article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.