Data structure-5-hash table

Five, hash table

1. Concept

A hash table is a data structure with a mapping relationship, that is, given a KEY, the corresponding VALUE can be found. KEY and VALUE form an Entry (or Node)

KEY is unique. If the entry added later is equal to the KEY of a previous Entry, only the new VALUE will overwrite the old VALUE, and a new Entry will not be created

In order to maintain all the entries, you can use the array structure. At the same time, in order to reduce the occupation 连续的内存空间, you can use the singly linked list structure to maintain the Entry corresponding to the KEY of the same hashCode value (in order to better improve the performance, you can also change the single Convert to a linked list to a binary tree, or restore a binary tree to a singly linked list)

2. Common methods

  1. put adds the element to the hash table (note that the entry of the equal KEY, the new VALUE will overwrite the old VALUE)
  2. get Find the corresponding VALUE according to the given KEY
  3. remove Remove the corresponding Entry according to the given KEY

3. Example

class HashTable<K, V> {
    
    
    private Entry<K, V>[] table;
    private final int length;
    private int size;

    public HashTable(int length) {
    
    
        this.length = length;
        this.size = 0;
    }

    public void put(K key, V value) {
    
    
        if (null == key || null == value) {
    
    
            throw new NullPointerException();
        }

        if (null == table) {
    
    
            @SuppressWarnings("unchecked")
            Entry<K, V>[] table = (Entry<K, V>[])new Entry[this.length];
            this.table = table;
        }

        int hash = hash(key);
        Entry<K, V> entry = table[hash];
        while (null != entry) {
    
    
            if (entry.key.equals(key)) {
    
    
                entry.value = value;
                return;
            }
            entry = entry.prev;
        }

        entry = table[hash];
        Entry<K, V> put = new Entry<K, V>(key, value);
        table[hash] = put;
        put.prev = entry;

        this.size++;
    }

    public V get(K key) {
    
    
        if (null == key) {
    
    
            throw new NullPointerException();
        }

        int hash = hash(key);
        Entry<K, V> entry = table[hash];
        while (null != entry) {
    
    
            if (entry.key.equals(key)) {
    
    
                return entry.value;
            }
            entry = entry.prev;
        }

        return null;
    }

    public boolean remove(K key) {
    
    
        if (null == key) {
    
    
            throw new NullPointerException();
        }

        int hash = hash(key);
        Entry<K, V> entry = table[hash];
        boolean first = true;
        Entry<K, V> next = table[hash];
        while (null != entry) {
    
    
            if (entry.key.equals(key)) {
    
    
                if (first) {
    
    
                    table[hash] = entry.prev;
                } else {
    
    
                    next.prev = entry.prev;
                }

                this.size--;
                return true;
            }

            entry = entry.prev;
            if (first) {
    
    
                first = false;
            } else {
    
    
                next = next.prev;
            }
        }

        return false;
    }

    public int size() {
    
    
        return this.size;
    }

    private int hash(K key) {
    
    
        return key.hashCode() % this.length;
    }

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

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

        @Override
        public String toString() {
    
    
            return this.key + "=" + this.value;
        }
    }

    @Override
    public String toString() {
    
    
        if (null == table) {
    
    
            return "{}";
        }

        StringBuilder sBuilder = new StringBuilder("{");
        for (int i = 0; i < this.length; i++) {
    
    
            Entry<K, V> entry = table[i];
            while (null != entry) {
    
    
                sBuilder.append(entry.toString()).append(',').append(' ');
                entry = entry.prev;
            }
        }

        return sBuilder.substring(0, sBuilder.length() - 2) + '}';
    }

}

Guess you like

Origin blog.csdn.net/adsl624153/article/details/103866147