fangga

Hashing

Jun 6th, 2023
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package Code.Hashing;
  2.  
  3. public class ClosedHashTable<K, V> {
  4.  
  5.     final int hashSize = 23;
  6.     private Entry<K, V>[] values;
  7.  
  8.     ClosedHashTable() {
  9.         values = new Entry[hashSize];
  10.     }
  11.  
  12.     private int hashFunction(K key) {
  13.         return Math.abs(key.hashCode()) % hashSize;
  14.     }
  15.  
  16.     // Metode untuk menyelesaikan tabrakan (collision) dengan linear probing
  17.     private int resolveCollision(K key, int index) {
  18.         int initialIndex = index;
  19.         while (values[index] != null) {
  20.             if (values[index].key.equals(key))
  21.                 return index;
  22.             index = (index + 1) % hashSize; // Linear probing
  23.             if (index == initialIndex)
  24.                 return -1;
  25.         }
  26.         return index;
  27.     }
  28.  
  29.     // Metode untuk mendapatkan indeks berdasarkan kunci menggunakan resolveCollision
  30.     private int getIndex(K key) {
  31.         return resolveCollision(key, hashFunction(key));
  32.     }
  33.  
  34.     // Metode untuk mencari indeks dengan kunci yang sesuai menggunakan linear probing
  35.     private int collidedIndex(K key) {
  36.         int initialIndex = hashFunction(key);
  37.         int index = initialIndex;
  38.         Entry<K, V> result = values[index];
  39.         if (result != null)
  40.             if (result.key.equals(key))
  41.                 return index;
  42.  
  43.         do {
  44.             do {
  45.                 index = (index + 1) % hashSize;
  46.                 if (index == initialIndex)
  47.                     return -1;
  48.             } while (values[index] == null);
  49.             result = values[index];
  50.         } while (!result.key.equals(key));
  51.         return index;
  52.     }
  53.  
  54.     // Metode untuk menambahkan elemen baru ke tabel hash
  55.     boolean put(K key, V value) {
  56.         int index = getIndex(key);
  57.         if (index == -1)
  58.             return false;
  59.         values[index] = new Entry<K, V>(key, value);
  60.         return true;
  61.     }
  62.  
  63.     // Metode untuk mendapatkan nilai (value) berdasarkan kunci (key)
  64.     V get(K key) {
  65.         int index = collidedIndex(key);
  66.         return index == -1 ? null : values[index].value;
  67.     }
  68.  
  69.     // Metode untuk mencetak isi tabel hash
  70.     void print() {
  71.         for (Entry<K, V> entry : values) {
  72.             if (entry != null)
  73.                 System.out.println(entry.toString());
  74.         }
  75.     }
  76. }
  77.  
  78. class Entry<K, V> {
  79.     K key;
  80.     V value;
  81.  
  82.     public Entry(K key, V value) {
  83.         this.key = key;
  84.         this.value = value;
  85.     }
  86.  
  87.     @Override
  88.     public String toString() {
  89.         return "{" + key.toString() + ":" + value.toString() + "}";
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment