Как реализовать удаление всех элементов со значением в hashmap
Не получается реализовать функцию удаления. Подаётся значение, все элементы с этим значением должны быть удалены, что я делаю не так?
package Project;
import java.util.*;
public class ReallyNewHashMap<K,V> extends AbstractMap<K,V> {
private HashMapper[] table;
private final int INITIAL_CAPASITY = 75;
private int capacity;
private double loadfactor;
private double treshold;
private int lenght;
public ReallyNewHashMap() {
loadfactor = 0.75;
capacity = INITIAL_CAPASITY;
table = new HashMapper[INITIAL_CAPASITY];
treshold = INITIAL_CAPASITY * loadfactor;
}
public void insert(K key,V value){
int hs =hash(key.hashCode()); // вычисляем хэш-код значения в таблице
int idx = indexFor(key.hashCode(),capacity); //вычисляем индекс Ноды в таблице
HashMapper e = new HashMapper(hs,key,value,null); // создаем ноду
if(table[idx] == null){
table[idx] = e;
lenght+=1;
refresh();
}else{
HashMapper temp = table[idx];
while(temp!=null){
if(e.hash== temp.hash &&(e.key == temp.key||(e.key).equals(temp.key) )){ /// ситуация если ключи не совпадают а хэши совпадают обработана здесь
temp.value = e.value;
return;
}
if(temp.next==null){
break;
}
temp = temp.next;
}
temp.next = e;
refresh();
}
}
public void delete(V value){
for(int i = 0; i < capacity; i++) {
if (table[i] != null) {
if(table[i].next==null){
if(table[i].value.equals((V)value)) {
table[i] = null;
continue;
}
}
HashMapper temp = table[i];
while (temp != null && temp.next != null ) {
while ( temp.next!=null && temp.next.value.equals((V) value)){
if(temp.next.next==null){
temp.next = null;
continue;
}
temp.next = temp.next.next;
}
if(temp.next!=null) {
temp = temp.next;
}
}
}
}
}
private static int indexFor(int h, int length) //h = key.hashCode() lenght = capacity
{
return h & (length - 1);
}
private static int hash(int h)
{
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
private class HashMapper<K, V> implements Map.Entry<K, V> {
private int hash;
private final K key;
private V value;
private HashMapper<K, V> next;
HashMapper(int hash, K key, V value, HashMapper<K, V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() {
return key;
}
public final V getValue() {
return value;
}
public final int hashCode() {
hash = 31;
hash = hash * 17 + key.hashCode();
return hash;
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
}
}