2013-04-28 110 views
0

所以我有一個哈希表類,它工作正常,但我想它打印出來一樣的內容,以便:在java中打印hashmap?

1 -> 101 -> 201 (this is a bucket for handling collision) 2 
3 - > 103 - > 203 
4 
5 

換句話說,我只是想知道如何讓我的程序打印出我的散列表的內容,使其看起來像那樣。任何意見或建議將不勝感激。我是哈希映射的新手,所以這很混亂。

而我不知道該怎麼做。

這是我的散列映射類,如果有幫助:

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++; 
    } 
    /** 
    * 
    * @param key 
    * @param value 
    * The delete method works similarly to the put method. 
    * It locates the desired value in the hash, e, 
    * and then it removes e from the bucket, like removing a node 
    * from a linked list. 
    * Then it sets the value of e to its next node. 
    * And then it decreases the size of the map. 
    */ 


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

     int keyBucket = hash(key); 

      MapEntry<K, V> e = Hash[keyBucket]; 

      while (e != null) { 
       if ((e.key == null && key == null) 
         || (e.key != null && e.key.equals(key))) { 
        e.value = value; 
        return; 
       } 
       e = e.next; 
      } 

      Hash[keyBucket] = new MapEntry<K, V>(key, value); 
      size--; 
     } 


    public void print(){ 
     //THIS IS WHERE I NEED HELP 
    } 

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

} } 

回答

0

您需要迭代您維護的數組作爲存儲桶並追加每個位置的條目內容。 這應該工作:

public void print() { 
    final StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < Hash.length; i++) { 
     sb.append(i).append(" : "); 

     MapEntry<K, V> temp = Hash[i]; 
     while (temp != null) { 
      sb.append(temp.key).append(" -> ").append(temp.value).append(" , "); 
      temp = temp.next; 
     } 

     sb.append("\n"); 
    } 
    System.out.println(sb.toString()); 
} 

它產生下面的輸出,例如:

0 : 180 -> 0.889234530714529 , 
1 : 
2 : 992 -> 0.11655748287282786 , 
3 : 
4 : 734 -> 0.8213900931007967 , 824 -> 0.8399483889863836 , 554 -> 0.7833733949735435 , 304 -> 0.9461472125123178 , 
5 : 865 -> 0.604963832544362 , 555 -> 0.1889914052365086 , 
6 : 536 -> 0.5835183387314298 , 
7 : 597 -> 0.3846960557011073 , 
8 : 
9 : 

您的put()delete()方法的錯誤。這應該可以解決您的put()方法:

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

    final MapEntry<K, V> newEntry = new MapEntry<K, V>(key, value); 
    newEntry.next = Hash[keyBucket]; // chain with current entry 
    Hash[keyBucket] = newEntry; 
    size++; 
} 
+0

或者,您可以當你循環時'System.out.print()'和'System.out.println()'的組合。這具有節省內存的優點。 – 2013-04-28 22:17:40

+1

@ Code-Guru是的,它可以節省內存。不幸的是以較慢的系統(IO)調用爲代價。 – shams 2013-04-29 00:26:56

+0

好點。這是規範時間與空間折中的另一個例子。 – 2013-04-29 14:03:11

0

首先,你應該覆蓋字符串返回的ToString()類的方法,而不是使用一個新的孔隙方法稱爲打印()。

public String toString() { 
    return "This is your output."; 
} 

關於你的問題,我不完全明白你想要打印什麼。你只是想爲你的散列圖中的每個條目打印「key」 - >「value」,或者你想實現不同的東西嗎?

+0

打印「鍵」 - >「價值」這正是我想做的事,但我要如何做到這一點,每鍵> – 2013-04-28 21:12:22

+0

如果你只是想你的輸出是「鍵」 - >「值」,這個問題歸結爲「我如何遍歷一個哈希映射」,請參閱http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap – scd 2013-04-28 21:18:33