Hi. I am very new to hashtable concept. Here is a skeleton of the methods I need to implement; BUT, my biggest question is the inner Entry class. Am I implementing the entry class correctly?Is my constructor correct? Thanks

public class HashTable<K, V> {

    /** define an inner class to hold your Entry objects */
    class Entry {
        int hash;
        protected K key;
        protected V value;
        Entry next;

        public Entry(int hash, K key, V value, Entry next){
            this.key = key;
            this.value = value;
            this.hash = hash;
            this.next = next;
        } 

        public K getKey(){
            return this.key;

        }

        public V getValue(){
            return this.value;
        }
    }



    private double loadFactor = 0.75;
    private final int defaultSize = 64;
    private Object buckets[] = new Object[ defaultSize ];
    private int numElements = 0;


    public HashTable() { 
        buckets = new Entry[defaultSize];

    }

    public void put( K key, V value ) {


    public V get( K key ) {


}

    public Iterator<E> keys() {


     public Iterator<V> values() {

}
}

Why not look atthe API doc for java.util.HashTable and its inner HashTable.Entry class. That will give you a good idea of what the public parts of a typical implementation look like.

For your Entry constructor, there's no need for the hash parameter because every key has it's own hash - overriding if appropriate the hashCode() method inherited from Object.

I don't know why you have a next variable - this isn't a list.

Because your buckets hold Entries you should declare it as Entry[], not Object[] (ps Java standards prefer Entry[] bucket rather than Entry bucket[] as the former better conveys the idea that there is one bucket variable that refers to an array of Entyries)

Line 48 <E> is not defined - presumably you ment <Entry>

Thanks. THe assignment I am working on is different from what I am reading from online tutorials and textbooks. So i was very confused

In the assignment prompt I am given the skeleton:

class Entry{ //fill }

Is there a way to make an Entry inner class without taking in something like Entry<K,V>?

Yes, that's OK. The <K> and <V> types are defined for the HashTable class, so they are automatically available and usable in any inner class of HashTable. There's no need or reason to re-define them for the inner class.

Thank you. Im going to try and implement this.

Hi. I am stuck. May I ask for help?

How can I create an array of Entry objects?

public class HashTbl<K, V> {

    /** define an inner class to hold your Entry objects */
    class Entry {

        protected K key;
        protected V value;

        public Entry(K key, V value){
            this.key = key;
            this.value = value;

        } 

        public K getKey(){
            return this.key;

        }

        public V getValue(){
            return this.value;
        }

        public V setValue(V val){
            V old = value;
            value = val;
            return old;

        }
    }

    private double loadFactor = 0.75;
    private final int defaultSize = 64;
    /**Error: Cannot create a generic array of HashTable<K,V>.Entry */
    private Entry[] buckets = new Entry[ defaultSize ];
    private int numElements = 0;


    public HashTbl() { 
        buckets = new Entry[defaultSize];

    }

40: buckets = new Entry[defaultSize];

You just did!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.