哈希表

什么是哈希表?


每一个字符都和一个索引相对应

如:int[26] freq 就是一个哈希表

哈希表_第1张图片

哈希表这种数据结构充分体现了用空间换时间

哈希函数的设计


“键”通过哈希函数得到的“索引”分布越均匀越好

设计方式如下:


哈希表_第2张图片

其他类型可以使用转成哈希函数转化成整形数据的方式

哈希冲突的处理方式


链地址法:


哈希表_第3张图片

哈希表实现


import java.util.TreeMap;

/**
 * Created by 53548 on 2019/9/8.
 */
public class HashTable< K, V> {

    private TreeMap[] hashtable;
    private int M;//哈希表具体有多少个位置
    private int size;

    public HashTable(int M){
        this.M = M;
        size = 0;
        hashtable = new TreeMap[M];
        for (int i = 0;i < M ; i++){
            hashtable[i] = new TreeMap<>();
        }
    }
    public HashTable(){
        this(97);
    }

    private int hash(K key){
        return (key.hashCode() & 0x7fffffff) % M;
    }

    public int getSize(){
        return size;
    }

    public void add(K key, V value){
        TreeMap map = hashtable[hash(key)];
        if(map.containsKey(key)){
            map.put(key,value);
        }
        else{
            map.put(key,value);
            size ++ ;
        }
    }

    public V remove(K key){
        TreeMap map = hashtable[hash(key)];
        V ret = null;
        if (map.containsKey(key)){
            ret = map.remove(key);
            size --;
        }
        return ret ;
    }

    public void set(K key,V value){
        TreeMap map = hashtable[hash(key)];

        if (map.containsKey(key)){
            throw new IllegalArgumentException(key + "doesn't exist");
        }
        map.put(key , value);
    }

    public boolean contains(K key){
        return hashtable[hash(key)].containsKey(key);
    }

    public V get(K key){
        return hashtable[hash(key)].get(key);
    }
}

哈希表动态空间处理


哈希表不像链表一样,
它每个内存空间只是储存一个哈希值
非元素,所以永远不会被填满扩容

所以我们应该制定一套自己的动态扩容方式:
如:
平均每个地址承载到了一定的程度,就开始扩容哈希表(N/M >= 某个设定的指标)
而每个地址中承载量少于指标一定程度,则开始缩容

public void add(K key, V value){
        TreeMap map = hashtable[hash(key)];
        if(map.containsKey(key)){
            map.put(key,value);
        }
        else{
            map.put(key,value);
            size ++ ;
//            扩容
            if(size >= upperTol *M){
                resize(2*M);
            }
        }
    }

    public V remove(K key){
        TreeMap map = hashtable[hash(key)];
        V ret = null;
        if (map.containsKey(key)){
            ret = map.remove(key);
            size --;
//            缩容
            if(size <= lowerTol *M && M / 2 >= initCapacity){
                resize(M / 2);
            }
        }
        return ret ;
    }

    private void resize(int newM){
        TreeMap[] newHashTable = new TreeMap[newM];
        for (int i = 0 ; i < newM;i++ ){
            newHashTable[i] = new TreeMap<>();
        }

        int oldM = M;
        this.M = newM;
        for (int i = 0; i < M; i++){
            TreeMap map = hashtable[i];
            for (K key : map.keySet()){
                newHashTable[hash(key)].put(key,map.get(key));
            }
        }

    }

动态空间处理优化


采用质数但却是接近2的整数倍的数能更好地分配哈希值

private final int[] capacity
            = {53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593,
            49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
            12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};

private int capacityIndex = 0;//默认开辟空间大小

public void add(K key, V value){
        TreeMap map = hashtable[hash(key)];
        if(map.containsKey(key)){
            map.put(key,value);
        }
        else{
            map.put(key,value);
            size ++ ;
//            扩容
            if(size >= upperTol *M && capacityIndex + 1 < capacity.length){
                capacityIndex ++;
                resize(capacity[capacityIndex]);
            }
        }
    }

    public V remove(K key){
        TreeMap map = hashtable[hash(key)];
        V ret = null;
        if (map.containsKey(key)){
            ret = map.remove(key);
            size --;
//            缩容
            if(size <= lowerTol *M && capacityIndex - 1 >= 0){
                capacityIndex --;
                resize(capacity[capacityIndex]);
            }
        }
        return ret ;
    }

你可能感兴趣的:(哈希表)