Java Collection Framework – An Exclusive Guide on Collection Framework
Collection Framework in Java is one of the fundamental parts of the Java programming language. Most of the programming languages majorly use the Collections.
Most of them support various types of Collections such as List, Set, Queue, Stack, etc. Today in this article we will learn about the Collections in Java and how to use Collection Frameworks in Java.
Moving forward in the article, we will also discuss the need for Collection Frameworks along with their advantages and will learn the hierarchy of Collection Framework in Java. For better understanding, we will discuss each Java Collection Framework with examples.
So, let’s start learning the concept of the Java Collection Framework.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
What is a Collection in Java?
Java Collection is a group of individual items in a single unit. Collections are like containers that merge multiple items into a single unit. For example, a bundle of sticks, a list of employee names, etc.
The two principal root interfaces of Java collection classes are Collection interface (java.util.Collection) and Map interface (java.util.Map).
Collection Framework in Java
A Collection Framework in Java is a unified architecture that represents a collection of interfaces and classes. It helps in storing and processing the data efficiently.
This framework has several useful classes that have a number of useful functions that make a programmer’s task easy. The Collection Frameworks represent and manipulate Collections in Java in a standard way.
Need for Collection Framework in Java
Before Java 1.2, Java provided some ad hoc classes like Vector, Properties, Stack, Hash Tables, Dictionary, etc to store and manipulate a group of objects.
These classes were useful but lacked a unified theme or central interface. For example, the way you used the Properties was different from the way you use Vector. Therefore, to overcome these problems, since JDK 1.2, the concept of Collection Framework was introduced in Java.
The collection framework met several goals like:
- It increases the efficiency of the fundamental collections like dynamic arrays, trees, linked lists, and hashtables, etc.
- Let the different types of collections work in an identical manner along with a higher degree of compatibility.
- Easily extend and/or adapt a collection.
- Remove the need for writing the code to implement the data structures and algorithms manually.
- Make our code much more efficient as the Collections Framework is highly optimized.
- Make our data unique we can use the Set interface provided by the Collections Framework.
- We can use the Map interface to store data in key/value pairs.
- Enable the functionality of resizable arrays, we can use the ArrayList class.
Composition of Java Collection Frameworks
All collections frameworks in Java include the following:
- Implementation or Classes
Java Collection Framework consists of interfaces which are abstract data types that represent collections. With Interfaces, we can manipulate the collections irrespective of the details of their representation. All the interfaces of the collections framework reside in java.util package.
In object-oriented languages, interfaces normally represent a hierarchy. The root or top-level interface of the Collection Framework is java.util.Collection. It contains some important methods such as add(), size(), remove(), clear(), iterator() that every Collection class must implement.
Some other important interfaces are java.util.List, java.util.Queue ,java.util.Set, and java.util.Map. The only interface that does not inherit the Collection interface is the Map interface but it is the part of the Collections framework.
Get to know more about Java Interface in detail with Techvidvan.
This section provides an overview of some interfaces in the Collection Framework:
|1||The Collection Interface||This interface is present at the top of the collections hierarchy and allows you to work with a group of objects.|
|2||The List Interface||This interface extends the Collection interface and the object of List stores an ordered collection of elements.|
|3||The Set Interface||This interface also extends the Collection interface and handles the sets that contain unique elements.|
|4||The SortedSet Interface||This interface extends the Set interface to handle the sorted sets.|
|5||The Map Interface||This interface maps the unique keys to values.|
|6||The SortedMap Interface||This interface extends the Map interface and maintains the keys in ascending order.|
|7||The Map.Entry Interface||It is an inner class of a Map and represents an element (a key/value pair) on a map.|
|8||The Enumeration Interface||It is a legacy interface that defines the methods by which you can enumerate the elements one at a time in a collection of objects.|
Java Collections framework provides implementation classes for collections which are the concrete implementations of the collection interfaces. In short, these classes are reusable data structures.
We can use them again and again to create different types of collections in Java code. Some important classes of collection framework are ArrayList, LinkedList, HashMap, TreeMap, HashSet, TreeSet.
These classes are more than enough to solve most of our requirements in programming, but if we still need some special collection class which we can extend to create our customized collection classes.
WAIT! It’s the right time to get familiar with the concept of Hierarchical Data Structures in Java in detail.
The following table summarizes the standard collection classes:
|1||AbstractCollection||This class implements most of the Collection interfaces.|
|2||AbstractList||This class extends the AbstractCollection class and implements most of the List interfaces.|
|3||AbstractSequentialList||This class extends the AbstractList class to use a collection that performs sequential access rather than random access to its elements.|
|4||LinkedList||This class implements a linked list by and extends the AbstractSequentialList class.|
|5||ArrayList||This class extends the AbstractList class and implements a dynamic array.|
|6||AbstractSet||This class extends the AbstractCollection class and implements most of the Set interface.|
|7||HashSet||This class extends the AbstractSet class to work with a hash table.|
|8||LinkedHashSet||This class extends the HashSet class and allows iterations in insertion-order.|
|9||TreeSet||This class extends the AbstractSet class and implements the Set stored in a tree.|
|10||AbstractMap||This class implements most of the Map interfaces.|
|11||TreeMap||This class extends the AbstractMap class to use a tree.|
|12||HashMap||This class extends the AbstractMap class to use a hash table.|
|13||WeakHashMap||This class extends the AbstractMap class and uses a hash table with weak keys.|
|14||LinkedHashMap||This class extends the HashMap class and allows iterations in insertion-order.|
|15||IdentityHashMap||This class Extends AbstractMap class and uses reference equality when comparing documents.|
An algorithm refers to the methods that perform useful computing operations, such as searching, sorting and shuffling on objects that implement collection interfaces.
The algorithms are polymorphic: that is, we can use the same method on several different implementations of the appropriate Java collection interface. We define these algorithms as static methods within the Collections class.
Enrich yourself with the knowledge of Java Polymorphism.
Hierarchy of Collection Framework in Java
We have learned that the Java collection framework contains interfaces and implementation classes. Now, let us see the hierarchy of the Java collections framework.
In the above diagram, the green boxes represent the different interfaces and the orange boxes represent the classes. The bold line represents that an interface extends another interface while a dashed line represents that a class implements an interface.
Advantages of Java Collection Framework
Following are some advantages of using Collection Frameworks in Java:
1. Consistent API: Java Collection Framework provides a consistent API that has an essential arrangement of interfaces like Collection, List, Set, List, Queue or Map. Each class, for example, ArrayList, LinkedList, Vector and so on, which implements these interfaces have some normal arrangement of strategies.
2. Reduces Programming Exertion: Using a Collection Framework, a software developer can focus on the best use of the program rather than focusing on an outline of Collection.
3. Increases Program Quality and Speed: Collection Framework helps to increase the execution speed and quality by giving the best use of valuable information structures and calculations.
Here comes the end of this article, we learned the basic concept of Java Collection Framework with its components and how they are arranged in the Collection Framework Hierarchy.
Also, we briefly discussed each interface and class present in the framework and explored the need and advantages of the Collection Framework in Java. Hope this article helped you to understand the concept of Collection Framework in Java.
Thank you for reading our article. If you have any queries related to Java Collection Framework, do let us know by dropping a comment below in the comment box.
Happy Learning 🙂