java学习笔记(4)-Hashtable类

1.几个变量值:

private transient Entry<?,?>[] table;
private transient int count;
private int threshold;
private float loadFactor;
private transient int modCount = 0;

对比上一篇介绍的HashMap,这几个变量表达的意思基本上差不多


2.构造函数:

    public Hashtable() {
        this(11, 0.75f);
    }

    public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }

    public Hashtable(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal Load: "+loadFactor);

        if (initialCapacity==0)
            initialCapacity = 1;
        this.loadFactor = loadFactor;
        table = new Entry<?,?>[initialCapacity];
        threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    }

    public Hashtable(Map<? extends K, ? extends V> t) {
        this(Math.max(2*t.size(), 11), 0.75f);
        putAll(t);
    }

注意和HashMap的区别:

Hashtable的初始容量是11,加载因子是0.75;HashMap初始容量是16,加载因子是0.75


3.Hashtable的put方法

    public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();//vauel不能为空
        }

        // Makes sure the key is not already in the hashtable.
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();//因为这里是直接用的hashCode,key是不能为空的
        int index = (hash & 0x7FFFFFFF) % tab.length;//取得index值
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {//遍历链表
            if ((entry.hash == hash) && entry.key.equals(key)) {//节点key相同,直接覆盖value
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);//节点不相同则添加
        return null;
    }


    private void addEntry(int hash, K key, V value, int index) {
        modCount++;

        Entry<?,?> tab[] = table;
        if (count >= threshold) {//hashtable键值对个数大于阈值,则扩容,重新计算hash,重新计算index
            // Rehash the table if the threshold is exceeded
            rehash();

            tab = table;
            hash = key.hashCode();
            index = (hash & 0x7FFFFFFF) % tab.length;
        }

        // Creates the new entry.
        @SuppressWarnings("unchecked")
        Entry<K,V> e = (Entry<K,V>) tab[index];
        tab[index] = new Entry<>(hash, key, value, e);//直接添加新节点
        count++;
    }



注意;

Hashtable中的value不能为null,key也不能为空,,Hashtable中的hash是直接取key的hashCode,下面是HashMap中的hash

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

Hashtable中取index方法是:(hash & 0x7FFFFFFF) % length

HashMap中取index方法是:(n - 1) & hash

Hashtable中的put方法是同步的


4.扩容

    @SuppressWarnings("unchecked")
    protected void rehash() {
        int oldCapacity = table.length;
        Entry<?,?>[] oldMap = table;

        // overflow-conscious code
        int newCapacity = (oldCapacity << 1) + 1;//扩容后的键值对大小为old*2+1
        if (newCapacity - MAX_ARRAY_SIZE > 0) {
            if (oldCapacity == MAX_ARRAY_SIZE)
                // Keep running with MAX_ARRAY_SIZE buckets
                return;
            newCapacity = MAX_ARRAY_SIZE;
        }
        Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

        modCount++;
        threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        table = newMap;

        for (int i = oldCapacity ; i-- > 0 ;) {
            for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
                Entry<K,V> e = old;
                old = old.next;

                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = (Entry<K,V>)newMap[index];
                newMap[index] = e;
            }
        }
    }


5.get方法

    @SuppressWarnings("unchecked")
    public synchronized V get(Object key) {
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return (V)e.value;
            }
        }
        return null;
    }

输入参数key,进行hash,找到index,遍历index中的链表,找到节点hash相等且key值相等的节点,返回其节点的value即是

该方法也是同步的,线程安全


总结(主要是与HashMap的不同):

1.Hashtable中key,value都不能是null;HashMap中key,value是可以为null的;

2.Hashtable中的大部分方法都是同步的,是线程安全的;HashMap中方法未进行同步,是线程不安全的;

3.HashMap默认的容量大小是16;增加容量时,每次将容量变为“原始容量x2”。
   Hashtable默认的容量大小是11;增加容量时,每次将容量变为“原始容量x2 + 1”

4.hash算法不同,Hashtable没有自定义hash算法:HashMap自定义了hash算法;

//Hashtable中的hash算法
int hash=key.hashCode();

//HashMap中的hash算法:
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }


5.取table数组中的index方式不同:

Hashtable中取index方法是:(hash & 0x7FFFFFFF) % length

HashMap中取index方法是:(n - 1) & hash

6.table中节点的处理方式不同:

Hashtable中hash冲突时,节点都是链表形式,而HashMap中在发生碰撞时,若链表长度大于默认值8则将链表转化成红黑树结构



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值