成都网站建设设计

将想法与焦点和您一起共享

HashTable源码分析

/**

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册雅安服务器托管、营销软件、网站建设、乡宁网站维护、网站推广。

  • The hash table data.
    */
    //存放键值对的数组
    private transient Entry[] table;

    /**

  • The total number of entries in the hash table.
    */
    //大小
    private transient int count;

    /**

  • The table is rehashed when its size exceeds this threshold. (The
  • value of this field is (int)(capacity * loadFactor).)
  • @serial
    */
    //阀值
    private int threshold;

    /**

  • The load factor for the hashtable.
  • @serial
    */
    //负载因子
    private float loadFactor;
    //默认初始容量为11,负载因子0.75
    public Hashtable() {
    this(11, 0.75f);
    }

    //1.8版本没有修改,没有维护红黑树结构
    public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
    throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry tab[] = table;
    int hash = key.hashCode();
    //求余获得索引
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry entry = (Entry)tab[index];
    for(; entry != null ; entry = entry.next) {
        if ((entry.hash == hash) && entry.key.equals(key)) {
            V old = entry.value;
            entry.value = value;
            return old;
        }
    }
    
    addEntry(hash, key, value, index);
    return null;

    }

    /**

  • Removes the key (and its corresponding value) from this
  • hashtable. This method does nothing if the key is not in the hashtable.
  • @param key the key that needs to be removed
  • @return the value to which the key had been mapped in this hashtable,
  • or null if the key did not have a mapping
  • @throws NullPointerException if the key is null
    */
    public synchronized V remove(Object key) {
    Entry tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;@SuppressWarnings("unchecked")
    br/>@SuppressWarnings("unchecked")
    e = (Entry)tab[index];
    for(Entry prev = null ; e != null ; prev = e, e = e.next) {
    if ((e.hash == hash) && e.key.equals(key)) {
    modCount++;
    if (prev != null) {
    prev.next = e.next;
    } else {
    tab[index] = e.next;
    }
    count--;
    V oldValue = e.value;
    e.value = null;
    return oldValue;
    }
    }
    return null;
    }

分享题目:HashTable源码分析
文章链接:http://chengdu.cdxwcx.cn/article/ihjpji.html