Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SolutionHashTable {
- public Entry[] table;
- public int capacity;
- /**
- * Constructs a new HashTable.
- *
- * Capacity of the hash table can not be 0 or negative.
- *
- * @param capacity
- * to be used as capacity of the table.
- * @throws IllegalArgumentException
- * if the input capacity is invalid.
- */
- public SolutionHashTable(int capacity) {
- if(capacity <= 0)throw new IllegalArgumentException();
- table = new Entry[capacity];
- this.capacity = capacity;
- }
- /**
- * Add a new Entry to the hash table,
- * uses linear probing to deal with collisions.
- *
- * Returns false, if the key is null or the table is full.
- *
- * @param key
- * String representing the key of the entry.
- * @param value
- * String representing the value of the entry.
- * @return true iff entry has been added successfully, else false.
- */
- public boolean put(String key, String value) {
- if(key == null)return false;
- int hash = hash(key);
- Entry e = new Entry(key,value);
- for(int i = 0; i < capacity; i++){
- Entry en = table[hash];
- if(en == null || (en.getKey() == null && en.getValue() == null) || en.getKey() == e.getKey()){
- table[hash] = e;
- return true;
- }
- hash = (hash+1) % capacity;
- }
- return false;
- }
- /**
- * Retrieve the value of the entry associated with this key.
- *
- * Returns null, if the key is null.
- *
- * @param key
- * String representing the key of the entry to look for.
- * @return value of the entry as String iff the entry with this key is found in the hash table, else null.
- */
- public String get(String key) {
- if(key == null)return null;
- int hash = hash(key);
- for(int i = 0; i < capacity; i++){
- if(table[hash] == null)return null;
- if(table[hash].getKey() == key){
- return table[hash].getValue();
- }
- hash = (hash+1) % capacity;
- }
- return null;
- }
- /**
- * Remove the entry associated with this key from the hash table.
- *
- * Returns false, if the key is null.
- *
- * @param key
- * String representing the key of the entry to remove.
- * @return true iff the entry has been successfully removed, else false.
- */
- public boolean remove(String key) {
- if(key == null)return false;
- int hash = hash(key);
- for(int i = 0; i < capacity; i++){
- if(table[hash] == null)return false;
- if(table[hash].getKey() == key){
- setDefunct(hash);
- return true;
- }
- hash = (hash+1) % capacity;
- }
- return false;
- }
- /**
- * Takes as input an index and sets the entry in that location as defunct.
- *
- * @param index
- * The index of the spot that is defunct.
- */
- public void setDefunct(int index) {
- this.table[index] = new Entry(null, null);
- }
- /**
- * Hashes a string representing a key.
- *
- * @param key
- * String that needs to be hashed.
- * @return the hashcode of the string, modulo the capacity of the HashTable.
- */
- public int hash(String key) {
- return Math.abs(key.hashCode()) % capacity;
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement