30 lines
951 B
Java
30 lines
951 B
Java
|
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;
|
||
|
}
|
||
|
}
|