Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to Java Collections Framework

DZone's Guide to

Introduction to Java Collections Framework

Want to learn more about using the Java collections framework? Check out this post about collections, as well as different Java classes and interfaces.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

In the earliest versions of Java (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, and the grouping of objects. These classes were lacking a central, 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 and 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:

  • Collection Interfaces — They provide abstract data types to represent collections. They allow for the manipulation of collections that are 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.

In any Java version after 2, it also supports Map interfaces and classes. Technically, Maps are not a part of collections in proper terms, but they are integrated with the 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 the Java collection framework is very vast. Here, we are going to cover the frequently used collection interfaces and classes. They are as follows:

  • Collection Interfaces —The following are the frequently used collection interfaces of the Java collection framework.

Image title


  • Collection Classes — The following are the frequently used collection classes of the Java collection framework.

Image title


Example

In the example shown below, we are going to create a data structure using the TreeMap class, which can store key and value pairs as elements. Here, the key is the 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 the TreeMap using the 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 the TreeSet.

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

Conclusion

In this article, we have discussed the Java collection framework and frequently used Java collection interfaces and classes, along with some fun examples!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java 1.1 ,java ,treemap ,collection ,java collection framework

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}