|
| 1 | +package com.bobocode.cs; |
| 2 | + |
| 3 | +import com.bobocode.util.ExerciseNotCompletedException; |
| 4 | + |
| 5 | +/** |
| 6 | + * {@link HashTable} is a simple Hashtable-based implementation of {@link Map} interface with some additional methods. |
| 7 | + * It is based on the array of {@link Node} objects. Both {@link HashTable} and {@link Node} have two type parameters: |
| 8 | + * K and V, which represent key and value. |
| 9 | + * <p> |
| 10 | + * Elements are stored int the table by their key. A table is basically an array, and fast access is possible due to |
| 11 | + * array capabilities. (You can access an array element by its index in O(1) time). In order to find an index for any |
| 12 | + * given key, it uses calculateIndex method which is based on the element's hash code. |
| 13 | + * <p> |
| 14 | + * If two elements (keys) have the same array index, they form a linked list. That's why class {@link Node} requires |
| 15 | + * a reference to the next field. |
| 16 | + * <p> |
| 17 | + * Since you don't always know the number of elements in advance, the table can be resized. You can do that manually by |
| 18 | + * calling method resizeTable, or it will be done automatically once the table reach resize threshold. |
| 19 | + * <p> |
| 20 | + * The initial array size (initial capacity) is 8. |
| 21 | + * |
| 22 | + * @param <K> - key type |
| 23 | + * @param <V> - value type |
| 24 | + * @author Taras Boychuk |
| 25 | + */ |
| 26 | +public class HashTable<K, V> implements Map<K, V> { |
| 27 | + |
| 28 | + /** |
| 29 | + * This method is a critical part of the hast table. The main idea is that having a key, you can calculate its index |
| 30 | + * in the array using the hash code. Since the computation is done in constant time (O(1)), it's faster than |
| 31 | + * any other kind search. |
| 32 | + * <p> |
| 33 | + * It's a function that accepts a key and calculates its index using a hash code. Please note that index cannot be |
| 34 | + * equal or greater than array size (table capacity). |
| 35 | + * <p> |
| 36 | + * This method is used for all other operations (put, get, remove). |
| 37 | + * |
| 38 | + * @param key |
| 39 | + * @param tableCapacity underlying array size |
| 40 | + * @return array index of the given key |
| 41 | + */ |
| 42 | + public static int calculateIndex(Object key, int tableCapacity) { |
| 43 | + throw new ExerciseNotCompletedException(); // todo: |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Adds an element to the hash table. Does not support duplicate elements. |
| 48 | + * |
| 49 | + * @param element |
| 50 | + * @return true if it was added |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public V put(K element, V value) { |
| 54 | + throw new ExerciseNotCompletedException(); // todo: |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Retrieves a value by the given key. It uses calculateIndex method to find the corresponding array index. |
| 59 | + * Then it iterates though all elements that are stored by that index, and uses equals to compare its keys. |
| 60 | + * |
| 61 | + * @param key |
| 62 | + * @return value stored in the table by the given key or null if there is no such key |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public V get(K key) { |
| 66 | + throw new ExerciseNotCompletedException(); // todo: |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Checks if the table contains a given key. |
| 71 | + * |
| 72 | + * @param key |
| 73 | + * @return true is there is such key in the table or false otherwise |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public boolean containsKey(K key) { |
| 77 | + throw new ExerciseNotCompletedException(); // todo: |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Checks if the table contains a given value. |
| 82 | + * |
| 83 | + * @param value |
| 84 | + * @return true is there is such value in the table or false otherwise |
| 85 | + */ |
| 86 | + @Override |
| 87 | + public boolean containsValue(V value) { |
| 88 | + throw new ExerciseNotCompletedException(); // todo: |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return a number of elements in the table. |
| 93 | + * |
| 94 | + * @return size |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public int size() { |
| 98 | + throw new ExerciseNotCompletedException(); // todo: |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Checks is the table is empty. |
| 103 | + * |
| 104 | + * @return true is table size is zero or false otherwise |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public boolean isEmpty() { |
| 108 | + throw new ExerciseNotCompletedException(); // todo: |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Removes an element by its key and returns a removed value. If there is no such key in the table, it returns null. |
| 113 | + * |
| 114 | + * @param key |
| 115 | + * @return removed value or null |
| 116 | + */ |
| 117 | + @Override |
| 118 | + public V remove(K key) { |
| 119 | + throw new ExerciseNotCompletedException(); // todo: |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * It's a special toString method dedicated to help you visualize a hash table. It creates a string that represents |
| 124 | + * an underlying array as a table. It has multiples rows. Every row starts with an array index followed by ": ". |
| 125 | + * Then it adds every key and value (key=value) that have a corresponding index. Every "next" reference is |
| 126 | + * represented as an arrow like this " -> ". |
| 127 | + * <p> |
| 128 | + * E.g. imagine a table, where the key is a string username, and the value is the number of points of that user. |
| 129 | + * Is this case method toString can return something like this: |
| 130 | + * <pre> |
| 131 | + * 0: johnny=439 |
| 132 | + * 1: |
| 133 | + * 2: madmax=833 -> leon=886 |
| 134 | + * 3: |
| 135 | + * 4: altea=553 |
| 136 | + * 5: |
| 137 | + * 6: |
| 138 | + * 7: |
| 139 | + * </pre> |
| 140 | + * |
| 141 | + * @return |
| 142 | + */ |
| 143 | + @Override |
| 144 | + public String toString() { |
| 145 | + throw new ExerciseNotCompletedException(); // todo: |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Creates a new underlying table with a given size and adds all elements to the new table. |
| 150 | + * <p> |
| 151 | + * In order to allow a fast access, this hash table needs to have a sufficient capacity. |
| 152 | + * (You can imagine a hash table, with a default capacity of 8 that stores hundreds of thousands of elements. |
| 153 | + * In that case it's just 8 huge linked lists. That's why we need this method.) |
| 154 | + * <p> |
| 155 | + * PLEASE NOTE that such method <strong>should not be a part of the public API</strong>, but it was made public |
| 156 | + * for learning purposes. You can create a table, print it using toString, then resizeTable and print it again. |
| 157 | + * It will help you to understand how it works. |
| 158 | + * |
| 159 | + * @param newCapacity a size of the new underlying array |
| 160 | + */ |
| 161 | + public void resizeTable(int newCapacity) { |
| 162 | + throw new ExerciseNotCompletedException(); // todo: |
| 163 | + } |
| 164 | +} |
0 commit comments