Results 1 to 2 of 2
  1. #1
    Growler is offline Member
    Join Date
    Jul 2010
    Rep Power

    Default Java Hash Tables


    I've been learning java... and wanted to practice implementing some data structures, and I'm currently trying to implement a hash table that implements a map interface.

    The class has been outlined in the following way:
    Java Code:
    public class HashTable<K,T> implements Map<K,T> {
    	protected HPair<K,T>[] _tab;         // protected so that subclass has access to it.
    	protected int   _sz;          		 // ditto.
    	public HashTable(int hs) { //Constructor
    		_tab = new HPair[hs]; 
    		_sz   = 0;
    I'm struggling on writing the methods for put(), get(), and remove().
    For put(), I currently have the following, which is throwing tons of errors:

    Java Code:
    	public T put(K key, T value) {
    		// TODO: Implement this method
    	    int hash = (key % hs);
    	    while (Hashtable[] != null && Hashtable[].getKey() != key)
    	    	hash = (hash + 1) % hs;
    	    Hashtable[] = new HashEntry(key, value);

    Java Code:
    	public T get(K key) {
    		int hash = (key % hs);
    		while (Hashtable[] != null && Hashtable[].getKey != key)
    			hash = (hash + 1) % hs;
    		if (Hashtable[] == null)
    			return -1;
    			return (Hashtable[].getValue());
    And for remove, I have no idea... The method stub should not be changed.
    Java Code:
    	public T remove(K key) {
    		// TODO: Implement this method
    		return null;
    Can anyone provide assistance?

    Last edited by Growler; 10-31-2010 at 04:22 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    Work on one method at a time, and write yourself a dump() method - to print the contents of the _tab array - that will allow you to test your methods as you write them.

    "tons of errors", "I have no idea" won't elicit the best help. Most folk here are very sympathetic but we require something precise and specific to work with (because we end up thinking like compilers maybe...)

    So if some specific method generates compiler messages that you can't understand, copy and post the complete message and indicate which line of your code it is referring to.

    As you work on the put() method make sure you have a clear plan of action: this may involve reading your textbook or whatever to see how the key parameter is used to determine where to put the data. Don't start coding until you are clear (and can clearly express) what it is you are trying to do.

    According to Wikipedia's account of Hash Tables both a hash function and some sort of collision strategy is required.

Similar Threads

  1. Hash Set in Java
    By muj in forum New To Java
    Replies: 14
    Last Post: 08-09-2010, 04:11 PM
  2. Java Tables
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2010, 05:03 PM
  3. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  4. Question about hash tables
    By behrk2 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:40 PM
  5. Java classes representing DB tables
    By aa74 in forum Advanced Java
    Replies: 1
    Last Post: 06-20-2008, 09:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts