Package dev.tylermong.customhashmap
Class CustomHashMap<K,V>
java.lang.Object
dev.tylermong.customhashmap.CustomHashMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
This class is a custom implementation of a
HashMap data structure using an array of linked lists for each bucket. This
class handles generic key-value pairs and provides basic public methods such as: put, get,
contains, and size. Internally, it has methods for hashing keys (simpleHash) and resizing the
map when the load factor exceeds a predefined threshold (resize).- Version:
- 1.0
- Author:
- Tyler Mong
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ArrayList<LinkedList<Entry<K, V>>> Buckets used to store the key-value pairs.private intThe capacity of the HashMap.private static final intThe initial capacity of the HashMap.private static final floatThe load factor threshold for resizing the HashMap.private intThe size of theHashMap, which is the number of key-value pairs currently stored in it. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyHashMapwith the default initial capacity of 16. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if this HashMap contains a mapping for the specified key.Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.private intHashes the specified key to an index in the buckets array.Associates the specified value with the specified key in thisHashMap.Removes the mapping for the specified key from thisHashMapif present.private voidresize()Resizes theHashMapby doubling its capacity and rehashing all existing entries.private intsimpleHash(K key) Deprecated.This method uses a simple hashing algorithm, based on the length of the key, which causes a high number of collisions.intsize()Returns the number of key-value mappings in thisHashMap.
-
Field Details
-
INITIAL_CAPACITY
private static final int INITIAL_CAPACITYThe initial capacity of the HashMap. "1 invalid input: '<'invalid input: '<' 4" is "1" (0001) bitwise left shifted 4 times, which is equal to 16 (10000). "1 invalid input: '<'invalid input: '<' 4" is used to signify that the initial capacity is a power of 2, which is important for the getting the index in thehashmethod.- See Also:
-
LOAD_FACTOR
private static final float LOAD_FACTORThe load factor threshold for resizing the HashMap.- See Also:
-
buckets
Buckets used to store the key-value pairs. Each bucket is a linked list of entries. -
capacity
private int capacityThe capacity of the HashMap. By default, it is set toINITIAL_CAPACITY, which is 16. The capacity is doubled when the load factor exceeds the threshold. -
size
private int sizeThe size of theHashMap, which is the number of key-value pairs currently stored in it. The size is incremented when a new key-value pair is added and decremented when a key-value pair is removed. It is also used to determine when to resize the HashMap.
-
-
Constructor Details
-
CustomHashMap
public CustomHashMap()Constructs an emptyHashMapwith the default initial capacity of 16. The size is initialized to 0, and each bucket is initialized as an empty linked list.
-
-
Method Details
-
put
Associates the specified value with the specified key in thisHashMap. If theHashMappreviously contained a mapping for the key, the old value is replaced. Resizes the map if the load factor exceeds the threshold.- Parameters:
key- the key to be inserted into theHashMapvalue- the value to be associated with the key- Returns:
- the old value associated with the key, or null if the key was not already present
-
get
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.- Parameters:
key- the key whose value is to be returned- Returns:
- the value to which the key is mapped, or
nullif the key is not found in theHashMap
-
remove
Removes the mapping for the specified key from thisHashMapif present. If the map contains a mapping for the key, the value is removed and returned. Otherwise, it returnsnull.- Parameters:
key- the key whose mapping is to be removed- Returns:
- the value to which the key was mapped, or
nullif the key is not found
-
simpleHash
Deprecated.This method uses a simple hashing algorithm, based on the length of the key, which causes a high number of collisions. Usehash(Object)instead for a more efficient hashing algorithm, which implements Java's standard HashMap hashing algorithm.Hashes the specified key to an index in the buckets array.- Parameters:
key- the key to be hashed- Returns:
- the length of the key, used as the index in the buckets array
-
hash
Hashes the specified key to an index in the buckets array. This implementation is based on Java's standard HashMap hashing algorithm, which uses the key's hashCode method and applies a bitwise XOR operation to reduce collisions. It also ensures that the index is within the bounds of the buckets array by applying a bitwise AND operation with the capacity minus one, also from Java's standard HashMap implementation.- Parameters:
key- the key to be hashed- Returns:
- the index in the buckets array where the key-value pair should be stored
-
contains
Returns true if this HashMap contains a mapping for the specified key.- Parameters:
key- the key whose presence in this HashMap is to be tested- Returns:
trueif this map contains a mapping for the specified key,falseotherwise
-
resize
private void resize()Resizes theHashMapby doubling its capacity and rehashing all existing entries. -
size
public int size()Returns the number of key-value mappings in thisHashMap.- Returns:
- the number of key-value mappings in this
HashMap
-