• Hashtable


    Hashtable也采用的链地址法

    package java.util;
    
    import java.io.*;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.function.BiConsumer;
    import java.util.function.Function;
    import java.util.function.BiFunction;
    import sun.misc.SharedSecrets;
    
    public class Hashtable<K,V>
        extends Dictionary<K,V>
        implements Map<K,V>, Cloneable, java.io.Serializable {
        
        public Hashtable() {
            this(11, 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;
            /* new Hashtable时直接创建Hashtable
             * 
             * new Hashtable时我们输入的容量是多少就会创建多大的数组
             * 而HashMap会转换为2的几次幂
             */
            table = new Entry<?,?>[initialCapacity];
            threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
        }
        // put方法是同步的
        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();
            // hashtable的哈希方法还用到了取模运算
            int index = (hash & 0x7FFFFFFF) % tab.length;
            @SuppressWarnings("unchecked")
            Entry<K,V> entry = (Entry<K,V>)tab[index];
            for(; entry != null ; entry = entry.next) {
                // 如果hash和key都相同,覆盖value的值
                if ((entry.hash == hash) && entry.key.equals(key)) {
                    V old = entry.value;
                    entry.value = value;
                    return old;
                }
            }
    
            addEntry(hash, key, value, index);
            return null;
        }    
        // 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;
        }   
        @SuppressWarnings("unchecked")
        protected void rehash() {
            int oldCapacity = table.length;
            Entry<?,?>[] oldMap = table;
    
            // overflow-conscious code
            // 扩展容量,2倍+1
            int newCapacity = (oldCapacity << 1) + 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;
                }
            }
        }    
    }

     Dictionary类

    Hashtable的get/put方法是从字典来的,虽然它也实现了Map接口,Map接口也有get/put方法

    public abstract
    class Dictionary<K,V> {
    
        public Dictionary() {
        }
    
        abstract public int size();
    
        abstract public boolean isEmpty();
    
        abstract public Enumeration<K> keys();
    
        abstract public Enumeration<V> elements();
    
        abstract public V get(Object key);
    
        abstract public V put(K key, V value);
    
        abstract public V remove(Object key);
    }
  • 相关阅读:
    任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0。
    【每天一个Linux命令】14. Linux中locate命令的用法
    ZetCode PyQt4 tutorial signals and slots
    ZetCode PyQt4 tutorial layout management
    ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window
    ZetCode PyQt4 tutorial First programs
    A Simple Makefile Tutorial
    Swapping eth0 and eth1 on OK335xS board
    OK335xS U-boot 环境变量解析
    OK335xS U-boot GPIO control hacking
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10431466.html
Copyright © 2020-2023  润新知