我需要实现一个可以从值中获取键并删除方法的方法

问题描述 投票:0回答:1

我有一个MapEntry类来实现Hashmap。我需要实现一个可以从值中获取键并删除方法的方法。我知道HashMap不会实现getValue这样的方法,但是我的教授要求这样做。我是编程新手,现在对我来说有点困难。我将不胜感激。

   public class MapEntry<K,V> {

   MapEntry<K,V> next;

    K key;

    V value;

    public MapEntry(K key, V value) {
         this.setKey(key);
         this.setValue(value);
    }
    public void setKey( K key){
        this.key=key;
    }
    public void setValue(V value){
        this.value=value;
    }
    public K getKey(){
        return key;
    }
    public V getValue(){
        return value;
    }
    public void setNext(MapEntry<K,V> next) {
        this.next = next;
    }

    public MapEntry<K, V> getNext() {
        return next;
    }
}
public class HashMap{

    private int DEFAULT_CAPACITY = 10;
    private MapEntry<String,Double>[] Hash;
    private int size;

    public HashMap() {
        Hash = new MapEntry[DEFAULT_CAPACITY];
    }

    public boolean isEmpty(){
        if(size!= 0){
            return false;
        }
        else{
            return true;
        }
    }



    public int getHashCode(String key){
        int bucketIndex = key.hashCode()%Hash.length;
        return bucketIndex;
    }

    public Double get(String key){
        if(key == null){
            try {
                throw new IllegalAccessException("Null key");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }


        MapEntry<String,Double> entry = Hash[getHashCode(key)];




        while (entry != null && !key.equals(entry.getKey()))
            entry = entry.getNext();
        if(entry != null)
            return entry.getValue();
        else
            return null;

   }
    }
    public void put(String key, double value){
        int keyBucket =hash(key);
        MapEntry<String,Double> temp = Hash[keyBucket];
        while (temp !=null){
            if((temp.key == null && key == null)
                || (temp.key != null && temp.key.equals(key))){
                temp.value = value;
                return;
            }
            temp = temp.next;
        }
        Hash[keyBucket] = new MapEntry<String, Double>(key,value);
        size++;
    }
    public void delete (String key) throws IllegalAccessException {
        if(key == null){
            throw new IllegalAccessException("Null key");
        }
    }


    private int hash(String key){
        if(key == null){
            return 0;
        }else {
            return Math.abs(key.hashCode()% this.Hash.length);
        }
    }

   public static void main(String[] args) {
 HashMap hashMap = new HashMap();
 hashMap.put("value", 2.2);
 hashMap.put("bob", 2.3);
     System.out.println(hashMap.get("value"));
     System.out.println(hashMap.get("bob"));
     System.out.println(hashMap.size);
     System.out.println(hashMap.getHashCode("value"));
     System.out.println(hashMap.getHashCode("bob"));
     System.out.println(hashMap.isEmpty());






    }
}
java dictionary hashmap implementation
1个回答
0
投票

我认为基本算法将是:

  • 一个接一个地迭代所有值
  • 如果您的值与所需结果匹配,请从该条目中检索密钥。
  • 要删除该条目,只需删除该条目

注意:如果有多个相同值的条目,则无法正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.