software-testing/project_task_sheets/phase_03/project_phase03_tasks/HashMap.java

30 lines
951 B
Java
Raw Normal View History

2022-05-18 16:40:39 +02:00
import java.util.Map.Entry;
public class HashMap<K,V> {
static final int MAXIMUM_CAPACITY = 1 << 10;
transient Entry<K,V>[] table;
int threshold;
final float loadFactor;
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
table = new Entry[capacity];
}
public int getCapacity() {
return table.length;
}
}