Java Program to implement Bi-Directional Map

«
»
This is a Java Program to implement Bi Directional Map. A bi-directional map is an associative data structure in which the (key, value) pairs form a one-to-one correspondence. Thus the binary relation is functional in each direction: value can also act as a key to key. A pair (a, b) thus provides a unique coupling between a and b so that b can be found when a is used as a key and a can be found when b is used as a key.

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

advertisement
Bi Directional Map Test
 
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
1
Enter key and value
green mango
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
1
Enter key and value
banana yellow
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
1
Enter key and value
red apple
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
2
Enter element
red
Result : apple
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
2
Enter element
mango
Result : green
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
2
Enter element
apple
Result : red
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
5
 
Size = 3
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
3
 
Enter element to be removed
red
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
2
Enter element
apple
 
Error : Not found
 
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
2
Enter element
red
 
Error : Not found
 
 
Do you want to continue (Type y or n)
 
y
 
Bi Directional Map <String, String> Operations
 
1. put
2. get
3. remove
4. clear
5. size
4
 
Bi Directional Map Cleared
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.