Java Collections Framework

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.

  1. What is an Array and its Limitation in Java?
  2. Definition and need of Java Collections Framework
  3. What are Collections?
  4. What is a Framework?
  5. Advantages of Java Collection Framework
  6. Collection Interfaces and its Hierarchy in Java
  7. Methods of Collection Interface in Java
  8. What is the need of collection framework classes?
  9. 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:
  1. The size of an array is fixed, it 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.
  2. 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.
  3. Array stores only a group of similar elements because of the problems in an array, Java comes up with 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 then 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 requirement. For example:

  1. The computer is a framework; it can be used by all people according to their requirements.
  2. Switchboard is a framework it can be used by all people according to their requirement.
  3. 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.

Collection Object

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 value we have to represent primitive value as object.

Collection Class

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 “java.util” (utility) package. All the collection interfaces and collection class 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:

  1. List: It is used to store a group of individual elements where the elements can be duplicated.
  2. Set: It is used to store a group of individual elements but the elements can’t be duplicated.
  3. 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.
  4. 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 this 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 collection in the file.

Collection stores a group of objects as follows:

Java Collections Framework

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.

Definition and need of Java Collections Framework

Advantages of Java Collection Framework:
  1. It is used as a temporary data storage (avoid hitting the database for getting the same amount of data.
  2. 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.
  3. 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.
  4. We do not have to write code to implement these data structures and algorithms manually.
  5. Our code will be much more efficient as the collections framework is highly optimized.
Collection Interfaces

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:

  1. List Interface
  2. Set Interface
  3. Queue Interface

Collection Interface Hierarchy in Java

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:

  1. add(Object): This method is used to add an object to the collection.
  2. addAll(Collection c): This method adds all the elements in the given collection to this collection.
  3. clear(): This method removes all of the elements from this collection.
  4. contains(Object o): This method returns true if the collection contains the specified element.
  5. containsAll(Collection c): This method returns true if all the collection contains all of the elements in the given collection.
  6. equals(Object o): This method compares the specified object with this collection for equality.
  7. hashCode(): This method is used to return the hash code value for this collection.
  8. isEmplty() : this method return true if this collection contains no elements.
  9. iterator(): This method returns an iterator over the elements in this collection.
  10. max(): This method is used to return the maximum value present in the collection.
  11. parallelStream(): This method returns a parallel Stream with this collection as its source.
  12. 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.
  13. removeAll(Collection c): This method is used to remove all the objects mentioned in the given collection from the collection.
  14. removeIf(Predicate filter): This method is used to removes all the elements of this collection that satisfy the given predicates.
  15. retainAll(Collection c): This method is used to retails only the elements in this collection that are contained in the specified collection.
  16. size(): This method is used to return the number of elements in the collection.
  17. spliterator(): This method is used to create a Spliterator over the elements in this collection.
  18. stream(): This method is used to return a sequential Stream with this collection as its source.
  19. toArray(): This method is used to return an array containing all of the elements in this collection.
What is the need of 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:

  1. 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.
  2. Then the controller layer application reads data from the collection object and fills it in view layer required code using HTML components.
  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *