Class AbstractHashMap<K,V>
- java.lang.Object
-
- AbstractMap<K,V>
-
- AbstractHashMap<K,V>
-
- All Implemented Interfaces:
- Map<K,V>
- Direct Known Subclasses:
- ChainHashMap, ProbeHashMap
public abstract class AbstractHashMap<K,V> extends AbstractMap<K,V>
An abstract base class supporting Map implementations that use hash tables with MAD compression. The base class provides the following means of support: 1) Support for calculating hash values with MAD compression 2) Support for resizing table when load factor reaches 1/2 Subclass is responsible for providing abstract methods: createTable(), bucketGet(h,k), bucketPut(h,k,v), bucketRemove(h,k), and entrySet() and for accurately maintaining the protected member, n, to reflect changes within bucketPut and bucketRemove.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.MapEntry<K,V>
-
-
Constructor Summary
Constructors Constructor and Description AbstractHashMap()
Creates a hash table with capacity 17 and prime factor 109345121.AbstractHashMap(int cap)
Creates a hash table with given capacity and prime factor 109345121.AbstractHashMap(int cap, int p)
Creates a hash table with the given capacity and prime factor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description protected abstract V
bucketGet(int h, K k)
Returns value associated with key k in bucket with hash value h.protected abstract V
bucketPut(int h, K k, V v)
Associates key k with value v in bucket with hash value h, returning the previously associated value, if any.protected abstract V
bucketRemove(int h, K k)
Removes entry having key k from bucket with hash value h, returning the previously associated value, if found.protected abstract void
createTable()
Creates an empty table having length equal to current capacity.V
get(K key)
Returns the value associated with the specified key, or null if no such entry exists.private int
hashValue(K key)
Hash function applying MAD method to default hash code.V
put(K key, V value)
Associates the given value with the given key.V
remove(K key)
Removes the entry with the specified key, if present, and returns its associated value.private void
resize(int newCap)
Updates the size of the hash table and rehashes all entries.int
size()
Tests whether the map is empty.-
Methods inherited from class AbstractMap
isEmpty
-
-
-
-
Constructor Detail
-
AbstractHashMap
public AbstractHashMap()
Creates a hash table with capacity 17 and prime factor 109345121.
-
AbstractHashMap
public AbstractHashMap(int cap)
Creates a hash table with given capacity and prime factor 109345121.
-
AbstractHashMap
public AbstractHashMap(int cap, int p)
Creates a hash table with the given capacity and prime factor.
-
-
Method Detail
-
size
public int size()
Tests whether the map is empty.- Returns:
- true if the map is empty, false otherwise
-
get
public V get(K key)
Returns the value associated with the specified key, or null if no such entry exists.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the associated value, or null if no such entry exists
-
remove
public V remove(K key)
Removes the entry with the specified key, if present, and returns its associated value. Otherwise does nothing and returns null.- Parameters:
key
- the key whose entry is to be removed from the map- Returns:
- the previous value associated with the removed key, or null if no such entry exists
-
put
public V put(K key, V value)
Associates the given value with the given key. If an entry with the key was already in the map, this replaced the previous value with the new one and returns the old value. Otherwise, a new entry is added and null is returned.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the key (or null, if no such entry)
-
hashValue
private int hashValue(K key)
Hash function applying MAD method to default hash code.
-
resize
private void resize(int newCap)
Updates the size of the hash table and rehashes all entries.
-
createTable
protected abstract void createTable()
Creates an empty table having length equal to current capacity.
-
bucketGet
protected abstract V bucketGet(int h, K k)
Returns value associated with key k in bucket with hash value h. If no such entry exists, returns null.- Parameters:
h
- the hash value of the relevant bucketk
- the key of interest- Returns:
- associate value (or null, if no such entry)
-
bucketPut
protected abstract V bucketPut(int h, K k, V v)
Associates key k with value v in bucket with hash value h, returning the previously associated value, if any.- Parameters:
h
- the hash value of the relevant bucketk
- the key of interestv
- the value to be associated- Returns:
- previous value associated with k (or null, if no such entry)
-
bucketRemove
protected abstract V bucketRemove(int h, K k)
Removes entry having key k from bucket with hash value h, returning the previously associated value, if found.- Parameters:
h
- the hash value of the relevant bucketk
- the key of interest- Returns:
- previous value associated with k (or null, if no such entry)
-
-