Implementation of Associative Array in Java

In this post, we will learn how to implement Associative Array in Java. An associative array is an array with named indexes. It consists of elements in form of key-value pairs where each key is unique and is associated with one value. In an associative array, each key-value pair appears at most once.

For example, consider a set of different fruits. We represent the color of different fruits using an array. We can use an associative array for this representation where fruits are the keys and the color of the fruit is the value. Note that in an associative array a key must be unique. However, two keys may be associated with the same values.

Java does not provide a direct implementation of Associative arrays but we can implement it using java.util.HashMap class.

To Learn about the HashMap class in Java, please go through this post: Working with HashMap (Insertion, Deletion, Iteration) in Java

 Implementing Associative Array in Java

Follow the steps given below to implement Associative Array in Java-

  • Create an instance of HashMap as given below.
    HashMap<String, String> hashmap = new HashMap<String, String>();
  • Insert key-value pairs in the hashmap using put() method.
    hashmap.put(key, value);
  • Convert the hashmap to set using entrySet() method and then convert the set s to an Array List as shown below.
    Set<Map.Entry<String ,String> > s = hashmap.entrySet();
    List<Map.Entry<String ,String>> array = new ArrayList<>(s);

See the below Java program that illustrates the working of Associative Array in Java.

import java.io.*;
import java.util.*;

public class Fruits{
    public static void main(String args[]){
        
        HashMap<String, String> hashmap = new HashMap<String, String>();
        hashmap.put("Apple", "Red");
        hashmap.put("Orange", "Orange");
        hashmap.put("Mango", "Yellow");
        
        Set<Map.Entry<String ,String> > s = hashmap.entrySet();
        
        List<Map.Entry<String ,String>> array = new ArrayList<>(s);
        
        for (int i=0; i<array.size(); i++) 
        { 
            System.out.println(array.get(i).getKey() + " is " + array.get(i).getValue()); 
        } 
        
    }
    
}

Output:

Apple is Red
Mango is Yellow
Orange is Orange

Now, have a look at this Java program which is given below.

import java.io.*;
import java.util.*;

public class Fruits{
    public static void main(String args[]){
        
        HashMap<String, String> hashmap = new HashMap<String, String>();
        hashmap.put("Apple", "Red");
        hashmap.put("Orange", "Orange");
        hashmap.put("Mango", "Yellow");
        hashmap.put("Mango", "Green");
        
        Set<Map.Entry<String ,String> > s = hashmap.entrySet();
        
        List<Map.Entry<String ,String>> array = new ArrayList<>(s);
        
        for (int i=0; i<array.size(); i++) 
        { 
            System.out.println(array.get(i).getKey() + " is " + array.get(i).getValue()); 
        } 
        
    }
    
}

Output:

Apple is Red
Mango is Green
Orange is Orange

As you can see we tried to insert four key-value pairs in the associative array but since it cannot have two same keys, one of the key-value pairs is discarded.

Thank you.

You may also read: Implementing Count-Min Sketch in Java

Leave a Reply

Your email address will not be published.