Java ProgrammingIntroduction to Java Collections Framework

Introduction to 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 Interface The collection interface is at the top of the collection hierarchy. It allows working with groups of objects.
2. The List Interface The List interface extends Collection interface. A List instance stores an ordered collection of elements.
3. The Enumeration The 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 Map The Map interface is used to map unique keys to associated values. It represents key-value pair collection of Objects.
5. The Map.Entry The Map.Entry is an element in a map which is a key-value pair. It is an inner class of Map.
6. The Set The Set interface extends Collections for handling sets. A Set always contains unique elements.
7. The SortedSet The SortedSet interface extends Set interface which enables to handle sorted sets.
8. The SortedMap The 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. AbstractCollection AbstractCollection class implements most of the Collection interfaces.
2. AbstractList AbstractList class extends AbstractCollection class and implements most of the List interfaces.
3. AbstractSequentialList AbstractSequentialList class extends AbstractList class which is used by a collection that uses sequential access of its elements instead of random access.
4. LinkedList LinkedList class extends AbstractSequentialList class in order to Implement a linked list.
5. ArrayList ArrayList class extends AbstractList class in order to implement a dynamic array.
6. AbstractSet AbstractSet class extends AbstractCollection class and implements most of the Set interface.
7. HashSet HashSet class extends AbstractSet class in order to use it with a hash table.
8. LinkedHashSet LinkedHashSet class extends HashSet class to allow insertion-order iterations.
9. TreeSet TreeSet class extends AbstractSet class and implements a set stored in a tree.
10 AbstractMap AbstractMap class implements most of the Map interfaces.
11. HashMap HashMap class extends AbstractMap class and enables to use a hash table.
12. TreeMap TreeMap class extends AbstractMap class and enables to use a tree.
13. WeakHashMap WeakHashMap class extends AbstractMap class and enables to use a hash table with weak keys.
14. LinkedHashMap LinkedHashMap class extends HashMap class and enables to allow insertion-order iterations.
15. IdentityHashMap IdentityHashMap 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.

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
 * 
 * @author Aparajita
 *
 */
public class TreeMapExample {

	public static void main(String args []) {
		// Create a hash map
		Map<String, Integer> treeMap = new TreeMap<String, Integer> ();

		// Put elements to the map
		treeMap.put ("Rohan", new Integer (15));
		treeMap.put ("Mohit", new Integer (32));
		treeMap.put ("Hari", new Integer (44));
		treeMap.put ("Abhi", new Integer (12));
		treeMap.put ("Wasim", new Integer (67));

		// Get a set of the entries
		Set<String> set = treeMap.keySet();
		/**
		 * Tree map always keeps elements shorted based on Keys.
		 */
		for (String iteration: set) {
			System.out.println ("Current Element: " + treeMap.get (iteration));
		}
	}
}

Output

Current Element: 12
Current Element: 44
Current Element: 32
Current Element: 15
Current Element: 67

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -