Java Program to Implement Associative Array

An associative array stores elements as (key, value) pairs. It is a collection of unique keys, each associated with a specific value. It is also known as a map, is an abstract data type where each key appears at most once in the collection.

In Java, associative arrays can be implemented using the HashMap class, that provides a flexible and efficient way to manage key-value pairs.

Syntax

Steps to Implement Associative Array

Initialize the Map:

Add Key-Value Pairs:

Convert Map to Set:

Convert Set to List:

File Name: AssociativeArrayExample.java

Output:

 
Size of map: 3
course: Java
author: John Doe
language: English   

Using Iterator

You can also iterate through the associative array using the iterator() method:

File Name: AssociativeArrayWithIterator.java

Output:

 
Size of map: 3
Age=25
ID=101
Marks=90   

Conclusion

Implementing an associative array in Java using HashMap is straightforward and efficient. The HashMap class provides various methods to add, retrieve, iterate, and remove key-value pairs, making it a versatile tool for managing collections of data. By following the steps outlined above, you can easily handle associative arrays in your Java programs, allowing for efficient data management and retrieval.