数据结构-哈希表

栏目: 数据库 · 发布时间: 7年前

内容简介:在Java8开始,当哈希冲突达到一定的程度,每一个位置从链表转化为红黑树。

哈希冲突的解决方法 链地址法

Java 8开始,当哈希冲突达到一定的程度,每一个位置从链表转化为红黑树。

时间复杂度分析

数据结构-哈希表

哈希表的动态空间处理

  1. 平均每个地址承载的元素多过一定程度,即 扩容 (N/M >= upperTol)
  2. 平均每个地址承载的元素少过一定程度,即 缩容 (N/M <= lowerTol)

哈希表复杂度分析

数据结构-哈希表

刚开始我们在扩容的时候直接是2*M,它可能造成扩容后的哈希表分布不均匀,可以按着下面这个表格来设置M值。

数据结构-哈希表

代码实现

public class HashTable<K, V> {

    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 static final int upperTol = 10;
    private static final int lowerTol = 2;
    private static final int initCapacity = 7;
    private int CapacityIndex = 0;
    
    private TreeMap<K, V>[] hashtable;
    private int size;
    private int M;    //hash表的长度,即具体有多少个位置(选择一个合适的素数)

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

    /*public HashTable(){
        this(initCapacity);
    }*/

    private int hash(K key){
        //key.hashCode() & 0x7fffffff 取key.hashCode()的绝对值
        return (key.hashCode() & 0x7fffffff) % M;
    }

    public int getSize(){
        return size;
    }

    public void add(K key, V value){
        TreeMap<K, V> 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)    //即size除以M >=upperTol
                //resize(2 * M);
                CapacityIndex ++;
                resize(capacity[CapacityIndex]);
        }
    }

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

    public void set(K key, V value){
        TreeMap<K, V> 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);
    }
    
    private void resize(int newM){
        TreeMap<K, V>[] newHashTable = new TreeMap[newM];
        for(int i = 0 ; i < newM ; i ++)
            newHashTable[i] = new TreeMap<>();
            
        //由于在hash()方法中有对M进行操作,在往新哈希表中存数据时应该用newM计算hash相应的hash值
        int oldM = M;
        this.M = newM;
        
        for(int i = 0 ; i < oldM ; i ++){
            TreeMap<K, V> map = hashtable[i];
            for(K key: map.keySet())
                newHashTable[hash(key)].put(key, map.get(key));
        }

        this.hashtable = newHashTable;
    }
}

哈希表的均摊复杂度为O(1),有这么好的性能其中一个原因是它牺牲了 顺序性


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Mathematica Cookbook

Mathematica Cookbook

Sal Mangano / O'Reilly Media / 2009 / GBP 51.99

As the leading software application for symbolic mathematics, Mathematica is standard in many environments that rely on math, such as science, engineering, financial analysis, software development, an......一起来看看 《Mathematica Cookbook》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具