0

So I have a hash map class, and it works fine, but I want it to print out the contents like so:

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

In other words, I would simply like to know how to get my program to print out the contents of my hash table so that it looks like that. Any advice or suggestions would be very appreciated. I'm new to hash maps, so this is very confusing.

And I'm not sure how to do it.

This is my hash map class if it helps:

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

}   }
Maria Stewart
  • 75
  • 2
  • 4
  • 9

2 Answers2

0

For one, you should overwrite the String returning .toString() method of your class instead of using a new void method called print().

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

Concerning your question, I don't fully understand what you want to be printed. Do you just want to print "key" -> "value" for each entry in your hash map or do you want to achieve something different?

scd
  • 164
  • 1
  • 2
  • 14
  • Printing "key" -> "value" is exactly what I want to do, but how do I do it for every key> – Maria Stewart Apr 28 '13 at 21:12
  • If you just want your output be "key"->"value", the question boils down to "How do I iterate over a hashmap", see http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap – scd Apr 28 '13 at 21:18
0

You need to iterate through the array you maintain as the buckets and append the contents of the entries at each location. This should work:

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

It produces the following output for example:

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 : 

You have a bug in your put() and delete() methods. This should fix your put() method:

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++;
}
shams
  • 3,460
  • 24
  • 24