2013-04-28 129 views
0

所以一個hashmap是一個基於哈希的java地圖結構的實現。我已經想出瞭如何讓hashmap put方法起作用,但是我想編寫一個方法來刪除鍵值對,並且我在實現時遇到了麻煩。在Java Hashmap中實現remove方法?

我現在唯一能真正理解的唯一事情就是如何讓功能在鑰匙爲空或不存在的情況下停下來。我很樂意提供任何幫助。關於該方法如何工作的解釋,或者一些基本的僞代碼示例將非常感謝。

這是我在刪除方法至今:

public void delete(K key) { 
     if (key == null) { 
      throw new IllegalArgumentException("Null Key!"); 
     } 

     // Implement this method 

    } 

如果有幫助,這是我完成的映射條目類:

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; 
    } 

} 

而且,這裏是我的HashMap的全部類如果有幫助。

public class HashMap<K, V> { 

    private int DEFAULT_CAPACITY = 10; 
    private MapEntry<K, V>[] Hash; 
    private int size; 

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

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

    public V get(K key) { 
     if (key == null) { 
      throw new IllegalArgumentException("Null Key!"); 
     } 
     MapEntry<K, V> entry = Hash[getHashCode(key)]; 
     while (entry != null && !key.equals(entry.getKey())) 
      entry = entry.getNext(); 
     if (entry != null) 
      return entry.getValue(); 
     else 
      return null; 
    } 

/** 
    * 
    * @param key 
    * @param value 
    * The put method works by associating the specified value with 
    * the given key in the map. 
    * If the key is already in the map, 
    * the old value is replaced with the new one. 
    */ 


    public void put(K key, V value) { 
     int keyBucket = hash(key); 

     MapEntry<K, V> 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<K, V>(key, value); 
     size++; 
    } 

    public void delete(K key) { 
     if (key == null) { 
      throw new IllegalArgumentException("Null Key!"); 
     } 

     // Implement this method 

    } 


    public void print(){ 
     //Bonus Method 
    } 

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

} } 
+2

鑑於你正在複製一個有效的內置類,我建議你通過閱讀這門課來學習更多。 – 2013-04-28 18:53:44

回答

0

使用您在get()做同樣的邏輯,找到正確的桶和,該桶中,正確的MapEntry(姑且稱之爲e)。然後簡單地從存儲桶—中簡單地刪除e,這是從單鏈表中刪除一個節點。如果e是存儲桶中的第一個元素,請將相應元素Hash設置爲e.next;否則,將e之前的元素的next字段設置爲e.next。請注意,您需要多一個變量(因爲您發現e而更新)以跟蹤存儲區中的上一個條目。

+0

謝謝你的幫助。 – 2013-04-28 19:24:48