Java Program to Implement Hash Tables with Double Hashing

«
»
This is a Java Program to implement hash tables with Double Hashing. A hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. Double Hashing is a probe sequence in which the interval between probes is computed by another hash function.

Here is the source code of the Java program to implement hash tables with Double Hashing. 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 hash table with double hashing
  3.  */
  4. import java.util.Scanner;
  5. import java.math.*;
  6.  
  7. /* Class LinkedHashEntry */
  8. class HashEntry 
  9. {
  10.     String key;
  11.     int value;    
  12.  
  13.     /* Constructor */
  14.     HashEntry(String key, int value) 
  15.     {
  16.         this.key = key;
  17.         this.value = value;        
  18.     }
  19. }
  20.  
  21. /* Class HashTable */
  22. class HashTable
  23. {
  24.     private int TABLE_SIZE;
  25.     private int size; 
  26.     private HashEntry[] table;
  27.     private int primeSize;
  28.  
  29.      /* Constructor */
  30.     public HashTable(int ts) 
  31.     {
  32.         size = 0;
  33.         TABLE_SIZE = ts;
  34.         table = new HashEntry[TABLE_SIZE];
  35.         for (int i = 0; i < TABLE_SIZE; i++)
  36.             table[i] = null;
  37.         primeSize = getPrime();       
  38.     } 
  39.     /* Function to get prime number less than table size for myhash2 function */
  40.     public int getPrime()
  41.     {
  42.         for (int i = TABLE_SIZE - 1; i >= 1; i--)
  43.         {
  44.             int fact = 0;
  45.             for (int j = 2; j <= (int) Math.sqrt(i); j++)
  46.                 if (i % j == 0)
  47.                     fact++;
  48.             if (fact == 0)
  49.                 return i;
  50.         }
  51.         /* Return a prime number */
  52.         return 3;
  53.     }
  54.     /* Function to get number of key-value pairs */
  55.     public int getSize()
  56.     {
  57.         return size;
  58.     }
  59.     public boolean isEmpty()
  60.     {
  61.         return size == 0;
  62.     }
  63.     /* Function to clear hash table */
  64.     public void makeEmpty()
  65.     {
  66.         size = 0;
  67.         for (int i = 0; i < TABLE_SIZE; i++)
  68.             table[i] = null;
  69.     }
  70.     /* Function to get value of a key */
  71.     public int get(String key) 
  72.     {
  73.         int hash1 = myhash1( key );
  74.         int hash2 = myhash2( key );
  75.  
  76.         while (table[hash1] != null && !table[hash1].key.equals(key))
  77.         {
  78.             hash1 += hash2;
  79.             hash1 %= TABLE_SIZE;
  80.         }
  81.         return table[hash1].value;
  82.     }
  83.     /* Function to insert a key value pair */
  84.     public void insert(String key, int value) 
  85.     {
  86.         if (size == TABLE_SIZE)
  87.         {
  88.             System.out.println("Table full"); 
  89.             return;
  90.         }           
  91.         int hash1 = myhash1( key );
  92.         int hash2 = myhash2( key );        
  93.         while (table[hash1] != null)
  94.         {
  95.             hash1 += hash2;
  96.             hash1 %= TABLE_SIZE;
  97.         }
  98.         table[hash1] = new HashEntry(key, value);        
  99.         size++;
  100.     }
  101.     /* Function to remove a key */
  102.     public void remove(String key) 
  103.     {
  104.         int hash1 = myhash1( key );
  105.         int hash2 = myhash2( key );        
  106.         while (table[hash1] != null && !table[hash1].key.equals(key))
  107.         {
  108.             hash1 += hash2;
  109.             hash1 %= TABLE_SIZE;
  110.         }
  111.         table[hash1] = null;
  112.         size--;
  113.     }
  114.     /* Function myhash which gives a hash value for a given string */
  115.     private int myhash1(String x )
  116.     {
  117.         int hashVal = x.hashCode( );
  118.         hashVal %= TABLE_SIZE;
  119.         if (hashVal < 0)
  120.             hashVal += TABLE_SIZE;
  121.         return hashVal;
  122.     }
  123.     /* Function myhash function for double hashing */
  124.     private int myhash2(String x )
  125.     {
  126.         int hashVal = x.hashCode( );
  127.         hashVal %= TABLE_SIZE;
  128.         if (hashVal < 0)
  129.             hashVal += TABLE_SIZE;
  130.         return primeSize - hashVal % primeSize;
  131.     }
  132.     /* Function to print hash table */
  133.     public void printHashTable()
  134.     {
  135.         System.out.println("\nHash Table");
  136.         for (int i = 0; i < TABLE_SIZE; i++)
  137.             if (table[i] != null)
  138.                 System.out.println(table[i].key +" "+table[i].value);
  139.     }
  140. }
  141.  
  142. /* Class DoubleHashingHashTableTest */
  143. public class DoubleHashingHashTableTest
  144. {
  145.     public static void main(String[] args)
  146.     {
  147.         Scanner scan = new Scanner(System.in);
  148.         System.out.println("Hash Table Test\n\n");
  149.         System.out.println("Enter size");
  150.         /* Make object of HashTable */
  151.         HashTable ht = new HashTable(scan.nextInt() );
  152.  
  153.         char ch;
  154.         /*  Perform HashTable operations  */
  155.         do    
  156.         {
  157.             System.out.println("\nHash Table Operations\n");
  158.             System.out.println("1. insert ");
  159.             System.out.println("2. remove");
  160.             System.out.println("3. get");       
  161.             System.out.println("4. check empty");     
  162.             System.out.println("5. clear");
  163.             System.out.println("6. size");
  164.  
  165.             int choice = scan.nextInt();            
  166.             switch (choice)
  167.             {
  168.             case 1 : 
  169.                 System.out.println("Enter key and value");
  170.                 ht.insert(scan.next(), scan.nextInt() ); 
  171.                 break;                          
  172.             case 2 :                 
  173.                 System.out.println("Enter key");
  174.                 ht.remove( scan.next() ); 
  175.                 break;                        
  176.             case 3 : 
  177.                 System.out.println("Enter key");
  178.                 System.out.println("Value = "+ ht.get( scan.next() )); 
  179.                 break;                                   
  180.             case 4 : 
  181.                 System.out.println("Empty Status " +ht.isEmpty());
  182.                 break;
  183.             case 5 : 
  184.                 ht.makeEmpty();
  185.                 System.out.println("Hash Table Cleared\n");
  186.                 break;
  187.             case 6 : 
  188.                 System.out.println("Size = "+ ht.getSize() );
  189.                 break;         
  190.             default : 
  191.                 System.out.println("Wrong Entry \n ");
  192.                 break;   
  193.             }
  194.             /* Display hash table */
  195.             ht.printHashTable();  
  196.  
  197.             System.out.println("\nDo you want to continue (Type y or n) \n");
  198.             ch = scan.next().charAt(0);                        
  199.         } while (ch == 'Y'|| ch == 'y');  
  200.     }
  201. }

advertisement
Hash Table Test
 
 
Enter size
100
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
1
Enter key and value
prime 97
 
Hash Table
prime 97
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
1
Enter key and value
even 24
 
Hash Table
prime 97
even 24
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
1
Enter key and value
odd 63
 
Hash Table
prime 97
even 24
odd 63
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
1
Enter key and value
composite 6
 
Hash Table
prime 97
even 24
odd 63
composite 6
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
1
Enter key and value
armstrong 153
 
Hash Table
prime 97
even 24
odd 63
armstrong 153
composite 6
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
3
Enter key
prime
Value = 97
 
Hash Table
prime 97
even 24
odd 63
armstrong 153
composite 6
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
3
Enter key
even
Value = 24
 
Hash Table
prime 97
even 24
odd 63
armstrong 153
composite 6
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
2
Enter key
composite
 
Hash Table
prime 97
even 24
odd 63
armstrong 153
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
6
Size = 4
 
Hash Table
prime 97
even 24
odd 63
armstrong 153
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
5
Hash Table Cleared
 
 
Hash Table
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. check empty
5. clear
6. size
4
Empty Status true
 
Hash Table
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

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.