Programming & Development / April 15, 2025

HashTable Java Example: Custom Implementation with Separate Chaining

hashtable java hash map key-value pairs custom implementation separate chaining linked list put get remove hashCode hash function collision handling

This is a simple custom implementation of a hashtable in Java using separate chaining to handle collisions. Each index of the underlying array holds a LinkedList of entries (key-value pairs). It supports put, get, remove, and size operations.

Java Example:

java

import java.util.LinkedList;

public class HashTable<K, V> {
    private static final int DEFAULT_CAPACITY = 10;
    private LinkedList<Entry<K, V>>[] table;
    private int size;

    public HashTable() {
        this(DEFAULT_CAPACITY);
    }

    public HashTable(int capacity) {
        table = new LinkedList[capacity];
        for (int i = 0; i < capacity; i++) {
            table[i] = new LinkedList<>();
        }
        size = 0;
    }

    public void put(K key, V value) {
        int index = getIndex(key);
        LinkedList<Entry<K, V>> list = table[index];
        for (Entry<K, V> entry : list) {
            if (entry.getKey().equals(key)) {
                entry.setValue(value);
                return;
            }
        }
        list.add(new Entry<>(key, value));
        size++;
    }

    public V get(K key) {
        int index = getIndex(key);
        LinkedList<Entry<K, V>> list = table[index];
        for (Entry<K, V> entry : list) {
            if (entry.getKey().equals(key)) {
                return entry.getValue();
            }
        }
        return null;
    }

    public void remove(K key) {
        int index = getIndex(key);
        LinkedList<Entry<K, V>> list = table[index];
        for (Entry<K, V> entry : list) {
            if (entry.getKey().equals(key)) {
                list.remove(entry);
                size--;
                return;
            }
        }
    }

    public int size() {
        return size;
    }

    private int getIndex(K key) {
        int hash = key.hashCode();
        return Math.abs(hash % table.length);
    }

    private static class Entry<K, V> {
        private K key;
        private V value;

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

        public K getKey() { return key; }
        public V getValue() { return value; }
        public void setValue(V value) { this.value = value; }
    }

    public static void main(String[] args) {
        HashTable<String, Integer> hashTable = new HashTable<>();

        hashTable.put("apple", 5);
        hashTable.put("banana", 10);

        System.out.println(hashTable.get("apple"));   // Output: 5
        System.out.println(hashTable.get("banana"));  // Output: 10

        hashTable.put("apple", 7);
        System.out.println(hashTable.get("apple"));   // Output: 7

        hashTable.remove("apple");
        System.out.println(hashTable.get("apple"));   // Output: null
    }
}

Explanation:

  • The getIndex method hashes the key and calculates the index.
  • The Entry class holds key-value pairs.
  • Collisions are handled via linked lists at each index.
  • It supports updating values for existing keys, deleting entries, and tracking the total number of key-value pairs.

This approach mirrors how Java’s built-in HashMap handles collisions under the hood, though in production you'd prefer HashMap for performance and optimization features.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex