Дали задание написать простой HashMap c тестами
public class SimpleHashMap<K,V> implements Iterable
параметризован T почему то не показывает
private T[] array;
private int load;
private int modCount;
private int size;
public SimpleHashMap() {
this.array = new T[16];
load = (int) (array.length * 0.75);
}
public boolean insert(K key, V value) {
boolean result = false;
int index = -1;
T temp = new T(key, value);
index = hash(key);
if (size + 1 >= load) {
load *= 2;
expensive();
}
if (array[index] == null) {
array[index] = temp;
result = true;
size++;
modCount++;
}
return result;
}
public int getSize() {
return size;
}
private void expensive() {
T[] temp = array;
array = new T[temp.length * 2];
size = 0;
for (T tmp : temp) {
if (tmp != null) {
insert((K)tmp.getKey(), (V)tmp.getValue());
}
}
}
public V get(K key) {
return (V) array[hash(key)].getValue();
}
public boolean delete(K key) {
boolean result = false;
if (array[hash(key)] != null) {
array[hash(key)] = null;
size--;
modCount++;
result = true;
}
return result;
}
public int hash(K key) {
int temp = 31;
temp = temp * 17 + key.hashCode();
return temp % array.length;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int expectedModCount = modCount;
private int position = 0;
@Override
public boolean hasNext() {
return position < size;
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
} else if (expectedModCount != modCount) {
throw new ConcurrentModificationException();
}
return array[position++];
}
};
}
и тест
@Test
public void whenCreateIteratorThen() {
SimpleHashMap<Integer, String> map = new SimpleHashMap<>();
map.insert(12, "one");
map.insert(1, "two");
Iterator<T> it = map.iterator();
assertThat(it.hasNext(), is(true));
assertThat(it.next().getKey(), is(1));
assertThat(it.hasNext(), is(true));
System.out.println(it.hasNext());
assertThat(it.next().getKey(), is(12));
assertThat(it.hasNext(), is(false));
}
не пойму почему не работает итератор кидает NullPointerexception
. почему то не хочет показывать
