Introduction to Java Collections Framework

0
208
Java Collections Framework

In the earliest JAVA versions (before JAVA 2), there was support for collection objects such as Dictionary, Properties, Stack, and Vector. These Java classes were used for the storage, manipulation of objects and group of objects. These classes were lacking a central as well as unifying theme, as a result in the later versions of JAVA, the collection framework was designed to cater to the growing needs of data collection organization as well as management.

The JAVA collections framework is a robust, reliable, and high-performance mechanism that allows various collections to operate in every efficient degree of interoperability for data handling and management. Examples of such JAVA Collection interfaces and classes include dynamic arrays, linked lists, hash tables, trees, etc. The framework is designed in such a way that, it can easily enable extension and implementation of collection classes and interfaces respectively to the implementing class.

Each data collection implements a particular algorithm such as linked list, hash set, tree set, etc. which is underlying and can be inherited by the child class. The collection framework has two types of components. They are as follows.

  • Collection Interfaces – They provide abstract data types to represent collections. They allow manipulation of collections independent of their representation details. Since Java is an object-oriented language, it interfaces largely from a hierarchical structure.
  • Collection Classes – They provide concrete implementations to the collection interfaces. Actually, collection classes represent reusable data structures.

Algorithms in Java Collections framework

Each interface and class implements an underlying algorithm which is nothing but the way data is organized in the collection object. The algorithm helps the implementing objects to perform basic operations such as sorting, searching, etc. These algorithms are polymorphic in nature, which means that we can use the same method of the implementing interface on different implementations to perform supporting operations.

JAVA version after 2, also supports Map interfaces and classes. Technically, Maps are not a part of collections in proper term but they are integrated with JAVA Collection framework. A map is a type of interface that helps store keys and value pairs as data organization in the data structure.

Different types of collections

The scope of Java Collection framework is very vast, here we are going to cover the frequently used collection interfaces and classes, those are as follows:

  • Collection Interfaces
    The following are the frequently used Collection interfaces of JAVA Collection framework.

S No.

Interface

Description

1.The Collection InterfaceThe collection interface is at the top of the collection hierarchy. It allows working with groups of objects.
2.The List InterfaceThe List interface extends Collection interface. A List instance stores an ordered collection of elements.
3.The EnumerationThe Enumeration interface is a legacy interface. It defines the methods which allow enumerating the elements present in the collection of objects. It has been super-ceded by Iterator.
4.The MapThe Map interface is used to map unique keys to associated values. It represents key-value pair collection of Objects.
5.The Map.EntryThe Map.Entry is an element in a map which is a key-value pair. It is an inner class of Map.
6.The SetThe Set interface extends Collections for handling sets. A Set always contains unique elements.
7.The SortedSetThe SortedSet interface extends Set interface which enables to handle sorted sets.
8.The SortedMapThe SortedMap interface extends Map interface in order to maintain the keys in an ascending order.
  • Collection Classes
    The following are the frequently used Collection classes of JAVA Collection framework.

S No.

Class

Description

1.AbstractCollectionAbstractCollection class implements most of the Collection interfaces.
2.AbstractListAbstractList class extends AbstractCollection class and implements most of the List interfaces.
3.AbstractSequentialListAbstractSequentialList class extends AbstractList class which is used by a collection that uses sequential access of its elements instead of random access.
4.LinkedListLinkedList class extends AbstractSequentialList class in order to Implement a linked list.
5.ArrayListArrayList class extends AbstractList class in order to implement a dynamic array.
6.AbstractSetAbstractSet class extends AbstractCollection class and implements most of the Set interface.
7.HashSetHashSet class extends AbstractSet class in order to use it with a hash table.
8.LinkedHashSetLinkedHashSet class extends HashSet class to allow insertion-order iterations.
9.TreeSetTreeSet class extends AbstractSet class and implements a set stored in a tree.
10AbstractMapAbstractMap class implements most of the Map interfaces.
11.HashMapHashMap class extends AbstractMap class and enables to use a hash table.
12.TreeMapTreeMap class extends AbstractMap class and enables to use a tree.
13.WeakHashMapWeakHashMap class extends AbstractMap class and enables to use a hash table with weak keys.
14.LinkedHashMapLinkedHashMap class extends HashMap class and enables to allow insertion-order iterations.
15.IdentityHashMapIdentityHashMap class extends AbstractMap class and enables to use reference equality when comparing documents.

Example
In the below example, we are going to create a data structure using TreeMap class which can store key and value pairs as elements. Here the key is of string data type which stores name and value denotes integer data type that stores age. As we know, TreeMap stores keys in ascending order, therefore, names will be stored with the same. When we iterate over keys of TreeMap using Set interface and print the values from TreeMap then, we can notice that ages are printed on the output console based on the sorted names in TreeSet.

Below is the JAVA code.

Output

Source Code for this blog is here: java collection example

Delve deeper into Java including learning through examples with this tutorial video on the Java Collections Framework, where you’ll learn about interfaces and implementation classes such as lists, queues, sets, and much more:

Conclusion: –
In this article, we have discussed the JAVA Collection framework and frequently used JAVA collection interfaces and classes along with suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here