Java Program to Implement Hash Tables with Quadratic Probing

«
»
This is a Java Program to implement hash tables with Quadratic Probing. 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. Quadratic probing is a probe sequence in which the interval between probes is increased by adding the successive outputs of a quadratic polynomial to the starting value given by the original hash computation.

Here is the source code of the Java program to implement hash tables with Quadratic Probing. 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 Quadratic Probing Hash Table
  3.  **/ 
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class QuadraticProbingHashTable **/
  8. class QuadraticProbingHashTable
  9. {    
  10.     private int currentSize, maxSize;       
  11.     private String[] keys;   
  12.     private String[] vals;    
  13.  
  14.     /** Constructor **/
  15.     public QuadraticProbingHashTable(int capacity) 
  16.     {
  17.         currentSize = 0;
  18.         maxSize = capacity;
  19.         keys = new String[maxSize];
  20.         vals = new String[maxSize];
  21.     }  
  22.  
  23.     /** Function to clear hash table **/
  24.     public void makeEmpty()
  25.     {
  26.         currentSize = 0;
  27.         keys = new String[maxSize];
  28.         vals = new String[maxSize];
  29.     }
  30.  
  31.     /** Function to get size of hash table **/
  32.     public int getSize() 
  33.     {
  34.         return currentSize;
  35.     }
  36.  
  37.     /** Function to check if hash table is full **/
  38.     public boolean isFull() 
  39.     {
  40.         return currentSize == maxSize;
  41.     }
  42.  
  43.     /** Function to check if hash table is empty **/
  44.     public boolean isEmpty() 
  45.     {
  46.         return getSize() == 0;
  47.     }
  48.  
  49.     /** Fucntion to check if hash table contains a key **/
  50.     public boolean contains(String key) 
  51.     {
  52.         return get(key) !=  null;
  53.     }
  54.  
  55.     /** Functiont to get hash code of a given key **/
  56.     private int hash(String key) 
  57.     {
  58.         return key.hashCode() % maxSize;
  59.     }    
  60.  
  61.     /** Function to insert key-value pair **/
  62.     public void insert(String key, String val) 
  63.     {                
  64.         int tmp = hash(key);
  65.         int i = tmp, h = 1;
  66.         do
  67.         {
  68.             if (keys[i] == null)
  69.             {
  70.                 keys[i] = key;
  71.                 vals[i] = val;
  72.                 currentSize++;
  73.                 return;
  74.             }
  75.             if (keys[i].equals(key)) 
  76.             { 
  77.                 vals[i] = val; 
  78.                 return; 
  79.             }            
  80.             i = (i + h * h++) % maxSize;            
  81.         } while (i != tmp);       
  82.     }
  83.  
  84.     /** Function to get value for a given key **/
  85.     public String get(String key) 
  86.     {
  87.         int i = hash(key), h = 1;
  88.         while (keys[i] != null)
  89.         {
  90.             if (keys[i].equals(key))
  91.                 return vals[i];
  92.             i = (i + h * h++) % maxSize;
  93.             System.out.println("i "+ i);
  94.         }            
  95.         return null;
  96.     }
  97.  
  98.     /** Function to remove key and its value **/
  99.     public void remove(String key) 
  100.     {
  101.         if (!contains(key)) 
  102.             return;
  103.  
  104.         /** find position key and delete **/
  105.         int i = hash(key), h = 1;
  106.         while (!key.equals(keys[i])) 
  107.             i = (i + h * h++) % maxSize;        
  108.         keys[i] = vals[i] = null;
  109.  
  110.         /** rehash all keys **/        
  111.         for (i = (i + h * h++) % maxSize; keys[i] != null; i = (i + h * h++) % maxSize)
  112.         {
  113.             String tmp1 = keys[i], tmp2 = vals[i];
  114.             keys[i] = vals[i] = null;
  115.             currentSize--;  
  116.             insert(tmp1, tmp2);            
  117.         }
  118.         currentSize--;        
  119.     }       
  120.  
  121.     /** Function to print HashTable **/
  122.     public void printHashTable()
  123.     {
  124.         System.out.println("\nHash Table: ");
  125.         for (int i = 0; i < maxSize; i++)
  126.             if (keys[i] != null)
  127.                 System.out.println(keys[i] +" "+ vals[i]);
  128.         System.out.println();
  129.     }   
  130. }
  131.  
  132. /** Class QuadraticProbingHashTableTest **/
  133. public class QuadraticProbingHashTableTest
  134. {
  135.     public static void main(String[] args)
  136.     {
  137.         Scanner scan = new Scanner(System.in);
  138.         System.out.println("Hash Table Test\n\n");
  139.         System.out.println("Enter size");
  140.         /** maxSizeake object of QuadraticProbingHashTable **/
  141.         QuadraticProbingHashTable qpht = new QuadraticProbingHashTable(scan.nextInt() );
  142.  
  143.         char ch;
  144.         /**  Perform QuadraticProbingHashTable operations  **/
  145.         do    
  146.         {
  147.             System.out.println("\nHash Table Operations\n");
  148.             System.out.println("1. insert ");
  149.             System.out.println("2. remove");
  150.             System.out.println("3. get");            
  151.             System.out.println("4. clear");
  152.             System.out.println("5. size");
  153.  
  154.             int choice = scan.nextInt();            
  155.             switch (choice)
  156.             {
  157.             case 1 : 
  158.                 System.out.println("Enter key and value");
  159.                 qpht.insert(scan.next(), scan.next() ); 
  160.                 break;                          
  161.             case 2 :                 
  162.                 System.out.println("Enter key");
  163.                 qpht.remove( scan.next() ); 
  164.                 break;                        
  165.             case 3 : 
  166.                 System.out.println("Enter key");
  167.                 System.out.println("Value = "+ qpht.get( scan.next() )); 
  168.                 break;                                   
  169.             case 4 : 
  170.                 qpht.makeEmpty();
  171.                 System.out.println("Hash Table Cleared\n");
  172.                 break;
  173.             case 5 : 
  174.                 System.out.println("Size = "+ qpht.getSize() );
  175.                 break;         
  176.             default : 
  177.                 System.out.println("Wrong Entry \n ");
  178.                 break;   
  179.             }
  180.             /** Display hash table **/
  181.             qpht.printHashTable();  
  182.  
  183.             System.out.println("\nDo you want to continue (Type y or n) \n");
  184.             ch = scan.next().charAt(0);                        
  185.         } while (ch == 'Y'|| ch == 'y');  
  186.     }
  187. }

advertisement
Hash Table Test
 
 
Enter size
5
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
1
Enter key and value
N nitrogen y 1 O oxygen y 1 C carbon y 1 Cl chlorine y 1 Zn zinc
 
Hash Table:
N nitrogen
 
 
Do you want to continue (Type y or n)
 
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
Enter key and value
 
Hash Table:
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
Enter key and value
 
Hash Table:
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
Enter key and value
 
Hash Table:
Cl chlorine
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
Enter key and value
 
Hash Table:
Cl chlorine
Zn zinc
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
2
Enter key
Zn
i 1
 
Hash Table:
Cl chlorine
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
1
Enter key and value
Cl
chloro
 
Hash Table:
Cl chloro
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
2
Enter key
Cl
 
Hash Table:
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
5
Size = 3
 
Hash Table:
C carbon
N nitrogen
O oxygen
 
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. get
4. clear
5. size
4
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. clear
5. size
5
Size = 0
 
Hash Table:
 
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.