My own implementation of an hashtable This is my own implementation of an hashtable. I would like to receive some reviews or some feedbacks.

public class HashTableEntry&lt;K,V&gt; {

        private K key;
        private V value;
        private HashTableEntry&lt;K,V&gt; next;

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

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public HashTableEntry&lt;K, V&gt; getNext() {
            return next;
        }

        public void setNext(HashTableEntry&lt;K, V&gt; next) {
            this.next = next;
        }
}

public class HashTable&lt;K, V&gt; {

    private static int DEFAULT_CAPACITY = 31;

    private HashTableEntry&lt;K,V&gt;[] table;

    @SuppressWarnings("unchecked")
    public HashTable() {
        table = (HashTableEntry&lt;K,V&gt;[])new Object[DEFAULT_CAPACITY];
    }

    @SuppressWarnings("unchecked")
    public HashTable(int capacity) {
        table = (HashTableEntry&lt;K,V&gt;[])new Object[capacity];
    }

    public void put(K key, V value) {

        int index = getIndex(key);

        HashTableEntry&lt;K,V&gt; entry = getLastEntry(table[index]);

        entry.setNext(new HashTableEntry&lt;K,V&gt;(key, value));             
    }

    public V get(K key) {

        int index = getIndex(key);

        return findValue(table[index], key);
    }

    private int getIndex(K key) {

        return key.hashCode() % table.length;
    }

    private HashTableEntry&lt;K,V&gt; getLastEntry(HashTableEntry&lt;K,V&gt; entry) {

        if (entry == null) return null;

        HashTableEntry&lt;K,V&gt; currentEntry = entry;

        while(currentEntry.getNext() != null)
            currentEntry = currentEntry.getNext();

        return currentEntry;
    }

    private V findValue(HashTableEntry&lt;K,V&gt; entry, K key) {

        if (entry == null) return null;

        HashTableEntry&lt;K,V&gt; currentEntry = entry;

        while(currentEntry.getNext() != null) {

            if (currentEntry.getKey().equals(key)) {
                return currentEntry.getValue();
            }

            currentEntry = currentEntry.getNext();
        }

        return null;
    }
}
With two tightly related classes like HashTable and HashTableEntry, you should probably nest them inside each other:

public class HashTable&lt;K, V&gt; {
    public static class Entry&lt;K, V&gt; {
        ...
    }
    ...
}


This way, the relationship between the classes is clear.



In your Entry class, you make it mutable. However, why not make it immutable? Immutability wouldn't cost much, but it would make some stuff easier to keep track of, such as providing the user an immutable view of your hashtable (this may be undesired, but it's what I'd do).

public static class Entry&lt;K,V&gt; { // This was nested within HashTable&lt;K, V&gt;
    private final K key;
    private final V value;
    private final Entry&lt;K,V&gt; next;

    public Entry(K key, V value, Entry&lt;K, V&gt; next) {
        this.key = key;
        this.value = value;
        this.next = next; // This could be changed by the user, so we aren't quite immutable yet; we'd have to do a deep copy on `next`
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public Entry&lt;K, V&gt; getNext() {
        return next;
    }

    public Entry&lt;K, V&gt; withNext(Entry&lt;K, V&gt; newNext) {
        return new Entry&lt;&gt;(key, value, newNext);
    }
}


Interestingly, you are combining table entries with a linked-list. This makes sense from the implementation viewpoint of a hashtable, but for readability's sake, I recommend removing the Entry&lt;K, V&gt; next entirely and simply using an array of LinkedList in your HashTable class. If you don't want to use the built-in LinkedList class, I still feel it is clearer if you separate these two unrelated pieces of the hashtable.

This would require some other changes in the code as well.



In getLastEntry and findValue, you return null when the request cannot be met. However, this is a little bit unclear. I'd recommend instead using Optional&lt;Entry&lt;K, V&gt;&gt; and Optional&lt;V&gt; as your return types so you can just do this:

private Optional&lt;V&gt; findValue(HashTableEntry&lt;K,V&gt; entry, K key) {
    if (entry == null) return Optional.empty();

    HashTableEntry&lt;K,V&gt; currentEntry = entry;

    while(currentEntry.getNext() != null) {
        if (currentEntry.getKey().equals(key)) {
            return Optional.of(currentEntry.getValue());
        }

        currentEntry = currentEntry.getNext();
    }

    return Optional.empty();
}


Then to get the value, you'd call optional.get(), but first you should check optional.isPresent(). This speaks more clearly than a null, so I personally find it easier to follow in this case.