Java Program to Implement Hash Tables

«
»
This is a Java Program to implement hash tables. 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.

Here is the source code of the Java program to implement separate chaining hash tables. 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
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. class HashTable
  8. {
  9.     int[] arr;
  10.     int capacity;
  11.  
  12.     /** constructor **/
  13.     public HashTable(int capacity)
  14.     {
  15.         this.capacity = nextPrime(capacity);
  16.         arr = new int[this.capacity];
  17.     }
  18.  
  19.     /** function to insert **/
  20.     public void insert(int ele)
  21.     {
  22.         arr[ele % capacity] = ele;
  23.     }
  24.  
  25.     /** function to clear **/
  26.     public void clear()
  27.     {
  28.         arr = new int[capacity];
  29.     }
  30.  
  31.     /** function contains **/
  32.     public boolean contains(int ele)
  33.     {
  34.         return arr[ele % capacity] == ele;
  35.     }
  36.  
  37.     /** function to delete **/
  38.     public void delete(int ele)
  39.     {
  40.         if (arr[ele % capacity] == ele)
  41.             arr[ele % capacity] = 0;
  42.         else
  43.             System.out.println("\nError : Element not found\n");
  44.     }
  45.  
  46.     /** Function to generate next prime number >= n **/
  47.     private static int nextPrime( int n )
  48.     {
  49.         if (n % 2 == 0)
  50.             n++;
  51.         for (; !isPrime(n); n += 2);
  52.  
  53.         return n;
  54.     }
  55.  
  56.     /** Function to check if given number is prime **/
  57.     private static boolean isPrime(int n)
  58.     {
  59.         if (n == 2 || n == 3)
  60.             return true;
  61.         if (n == 1 || n % 2 == 0)
  62.             return false;
  63.         for (int i = 3; i * i <= n; i += 2)
  64.             if (n % i == 0)
  65.                 return false;
  66.         return true;
  67.     }
  68.  
  69.     /** function to print hash table **/
  70.     public void printTable()
  71.     {
  72.         System.out.print("\nHash Table = ");
  73.         for (int i = 0; i < capacity; i++)
  74.             System.out.print(arr[i] +" ");
  75.         System.out.println();
  76.     }
  77. }
  78.  
  79. /** Class HashTableTest **/
  80. public class HashTableTest
  81. {
  82.     public static void main(String[] args)
  83.     {
  84.         Scanner scan = new Scanner(System.in);
  85.         System.out.println("Hash Table Test\n\n");
  86.         System.out.println("Enter size");
  87.         /** Make object of HashTable **/
  88.         HashTable ht = new HashTable(scan.nextInt() );
  89.  
  90.         char ch;
  91.         /**  Perform HashTable operations  **/
  92.         do    
  93.         {
  94.             System.out.println("\nHash Table Operations\n");
  95.             System.out.println("1. insert ");
  96.             System.out.println("2. remove");
  97.             System.out.println("3. contains");            
  98.             System.out.println("4. clear");            
  99.  
  100.             int choice = scan.nextInt();            
  101.             switch (choice)
  102.             {
  103.             case 1 : 
  104.                 System.out.println("Enter integer element to insert");
  105.                 ht.insert( scan.nextInt() ); 
  106.                 break;                          
  107.             case 2 :                 
  108.                 System.out.println("Enter integer element to delete");
  109.                 ht.delete( scan.nextInt() ); 
  110.                 break;                        
  111.             case 3 : 
  112.                 System.out.println("Enter integer element to check if present");
  113.                 System.out.println("Contains : "+ ht.contains(scan.nextInt() ));
  114.                 break;                                   
  115.             case 4 : 
  116.                 ht.clear();
  117.                 System.out.println("Hash Table Cleared\n");
  118.                 break;
  119.             default : 
  120.                 System.out.println("Wrong Entry \n ");
  121.                 break;   
  122.             }
  123.             /** Display hash table **/
  124.             ht.printTable();  
  125.  
  126.             System.out.println("\nDo you want to continue (Type y or n) \n");
  127.             ch = scan.next().charAt(0);                        
  128.         } while (ch == 'Y'|| ch == 'y');  
  129.     }
  130. }

advertisement
Hash Table Test
 
 
Enter size
10
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
28
 
Hash Table = 0 0 0 0 0 0 28 0 0 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
24
 
Hash Table = 0 0 24 0 0 0 28 0 0 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
14
 
Hash Table = 0 0 24 14 0 0 28 0 0 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
19
 
Hash Table = 0 0 24 14 0 0 28 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
94
 
Hash Table = 0 0 24 14 0 0 94 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
17
 
Hash Table = 0 0 24 14 0 0 17 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
3
Enter integer element to check if present
94
Contains : false
 
Hash Table = 0 0 24 14 0 0 17 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
3
Enter integer element to check if present
17
Contains : true
 
Hash Table = 0 0 24 14 0 0 17 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
2
Enter integer element to delete
17
 
Hash Table = 0 0 24 14 0 0 0 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
1
Enter integer element to insert
94
 
Hash Table = 0 0 24 14 0 0 94 0 19 0 0
 
Do you want to continue (Type y or n)
 
y
 
Hash Table Operations
 
1. insert
2. remove
3. contains
4. clear
4
Hash Table Cleared
 
 
Hash Table = 0 0 0 0 0 0 0 0 0 0 0
 
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.