Java Program to Implement Associate Array

This is a Java Program to implement Associate Array. An associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

Here is the source code of the Java Program to implement Associate Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to implement Associate Array
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.HashMap;
  7.  
  8. /** class AssociateArray */
  9. class AssociateArray
  10. {
  11.     private HashMap<String, String> keyVal;   
  12.  
  13.     /** constructor **/
  14.     public AssociateArray()
  15.     {
  16.         keyVal = new HashMap<String, String>();        
  17.     }    
  18.     /** function to clear **/
  19.     public void clear()
  20.     {
  21.         keyVal.clear();        
  22.     }
  23.     /** function to get size **/
  24.     public int size()
  25.     {
  26.         return keyVal.size();
  27.     }   
  28.     /** function to insert element **/
  29.     public void insert(String key, String val)
  30.     {
  31.         keyVal.put(key, val);        
  32.     }    
  33.     /** function to get element **/
  34.     public String get(String ele)
  35.     {
  36.         return keyVal.get(ele);        
  37.     }      
  38.     /** function to remove element **/
  39.     public void remove(String key)
  40.     {
  41.         keyVal.remove(key);              
  42.     } 
  43.     /** function to modify **/
  44.     public void modify(String key, String val)
  45.     {
  46.         keyVal.put(key, val);        
  47.     }        
  48. }
  49.  
  50. /** Class AssociateArrayTest **/
  51. public class AssociateArrayTest
  52. {
  53.     public static void main(String[] args)
  54.     {
  55.         Scanner scan = new Scanner(System.in);
  56.         System.out.println("Associate Array Test\n");   
  57.  
  58.         AssociateArray aa = new AssociateArray();
  59.  
  60.         char ch;
  61.         /*  Perform Associate Array operations */
  62.         do    
  63.         {
  64.             System.out.println("\nAssociate Array <String, String> Operations\n");
  65.             System.out.println("1. put ");
  66.             System.out.println("2. get");
  67.             System.out.println("3. remove");
  68.             System.out.println("4. modify");
  69.             System.out.println("5. clear");
  70.             System.out.println("6. size");
  71.  
  72.             int choice = scan.nextInt();            
  73.             switch (choice) 
  74.             {
  75.             case 1 : 
  76.                 System.out.println("Enter key and value");
  77.                 aa.insert(scan.next(), scan.next() );                     
  78.                 break;                          
  79.             case 2 : 
  80.                 System.out.println("Enter element");
  81.                 String ele = scan.next();
  82.                 String str = aa.get(ele);
  83.                 if (str != null)
  84.                     System.out.println("Result : "+ str);
  85.                 else
  86.                     System.out.println("\nError : Not found\n");  
  87.                 break;        
  88.             case 3 : 
  89.                 System.out.println("\nEnter key to be removed");
  90.                 aa.remove(scan.next() );
  91.                 break; 
  92.             case 4 : 
  93.                 System.out.println("\nEnter key, value to be modified");
  94.                 aa.modify(scan.next(), scan.next() );  
  95.                 break;                                                        
  96.             case 5 : 
  97.                 System.out.println("\nAssociate Array Cleared");
  98.                 aa.clear();
  99.                 break;    
  100.             case 6 : 
  101.                 System.out.println("\nSize = "+ aa.size() );
  102.                 break;         
  103.             default : 
  104.                 System.out.println("Wrong Entry \n ");
  105.                 break;   
  106.             }    
  107.  
  108.             System.out.println("\nDo you want to continue (Type y or n) \n");
  109.             ch = scan.next().charAt(0);                        
  110.         } while (ch == 'Y'|| ch == 'y');            
  111.     }
  112. }

Associate Array Test
 
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
1
Enter key and value
fruit mango
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
1
Enter key and value
vegetable tomato
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
1
Enter key and value
drink water
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
2
Enter element
drink
Result : water
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
2
Enter element
fruit
Result : mango
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
4
 
Enter key, value to be modified
fruit apple
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
2
Enter element
fruit
Result : apple
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
6
 
Size = 3
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
3
 
Enter key to be removed
fruit
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
2
Enter element
fruit
 
Error : Not found
 
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
5
 
Associate Array Cleared
 
Do you want to continue (Type y or n)
 
y
 
Associate Array <String, String> Operations
 
1. put
2. get
3. remove
4. modify
5. clear
6. size
6
 
Size = 0
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.